How to launch a form only once
-
Hi, folks. I’m developing an application in C# using Visual Studio. This application has a parent form from which the user can call other forms. My original problem was: if the user has already launched a form and he clicks again in the menu item to launch the same form, the program should only focus on the form. If the form is not on screen, the program should launch it. Another problem is that I have 1 form that is launched with 2 different data sets (different tables in a DB). I solved this problem this way: I declared 2 variables for the forms and defined 2 routines:
private void M2_SaidaEfetiva_Click(object sender, EventArgs e)
{
if (FormEfetivas == null)
{
FormEfetivas = new Frm_Saida();
FormEfetivas.MdiParent = this;
FormEfetivas.Text = "Saídas Efetivas";
FormEfetivas.Tag = "1";
FormEfetivas.Show();
}
FormEfetivas.Activate();
}
private void M2_SaidaPrevisao_Click(object sender, EventArgs e)
{
if (FormPrevisao == null)
{
FormPrevisao = new Frm_Saida();
FormPrevisao.MdiParent = this;
FormPrevisao.Text = "Previsão de Saídas";
FormPrevisao.Tag = "0";
FormPrevisao.Show();
}Now the problem is: when the user opens one form, then closes it, he cannot reopen it again, because the variables (FormEfetivas and FormPrevisao) are not eliminated when the user closes the form. What am I forgetting here? Can anybody help me? Thanks.
-
Hi, folks. I’m developing an application in C# using Visual Studio. This application has a parent form from which the user can call other forms. My original problem was: if the user has already launched a form and he clicks again in the menu item to launch the same form, the program should only focus on the form. If the form is not on screen, the program should launch it. Another problem is that I have 1 form that is launched with 2 different data sets (different tables in a DB). I solved this problem this way: I declared 2 variables for the forms and defined 2 routines:
private void M2_SaidaEfetiva_Click(object sender, EventArgs e)
{
if (FormEfetivas == null)
{
FormEfetivas = new Frm_Saida();
FormEfetivas.MdiParent = this;
FormEfetivas.Text = "Saídas Efetivas";
FormEfetivas.Tag = "1";
FormEfetivas.Show();
}
FormEfetivas.Activate();
}
private void M2_SaidaPrevisao_Click(object sender, EventArgs e)
{
if (FormPrevisao == null)
{
FormPrevisao = new Frm_Saida();
FormPrevisao.MdiParent = this;
FormPrevisao.Text = "Previsão de Saídas";
FormPrevisao.Tag = "0";
FormPrevisao.Show();
}Now the problem is: when the user opens one form, then closes it, he cannot reopen it again, because the variables (FormEfetivas and FormPrevisao) are not eliminated when the user closes the form. What am I forgetting here? Can anybody help me? Thanks.
Add a handler to the
Form.Closed
event when you create the instance, and in the handler set the appropriate variable (for exampleFormEfetivas
to null. The existing code will then generate a new instance when they try next time."I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
-
Add a handler to the
Form.Closed
event when you create the instance, and in the handler set the appropriate variable (for exampleFormEfetivas
to null. The existing code will then generate a new instance when they try next time."I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
Hi, OriginalGriff. I didn't follow you. Could you explain better? Or give me an example? Thanks.
-
Hi, folks. I’m developing an application in C# using Visual Studio. This application has a parent form from which the user can call other forms. My original problem was: if the user has already launched a form and he clicks again in the menu item to launch the same form, the program should only focus on the form. If the form is not on screen, the program should launch it. Another problem is that I have 1 form that is launched with 2 different data sets (different tables in a DB). I solved this problem this way: I declared 2 variables for the forms and defined 2 routines:
private void M2_SaidaEfetiva_Click(object sender, EventArgs e)
{
if (FormEfetivas == null)
{
FormEfetivas = new Frm_Saida();
FormEfetivas.MdiParent = this;
FormEfetivas.Text = "Saídas Efetivas";
FormEfetivas.Tag = "1";
FormEfetivas.Show();
}
FormEfetivas.Activate();
}
private void M2_SaidaPrevisao_Click(object sender, EventArgs e)
{
if (FormPrevisao == null)
{
FormPrevisao = new Frm_Saida();
FormPrevisao.MdiParent = this;
FormPrevisao.Text = "Previsão de Saídas";
FormPrevisao.Tag = "0";
FormPrevisao.Show();
}Now the problem is: when the user opens one form, then closes it, he cannot reopen it again, because the variables (FormEfetivas and FormPrevisao) are not eliminated when the user closes the form. What am I forgetting here? Can anybody help me? Thanks.
You loop through the existing Windows / forms before deciding to create an instance. [Get List of all Open Forms in Windows Application](https://www.c-sharpcorner.com/blogs/get-list-of-all-open-forms-in-windows-application1) Or, use a Mutex. [mutex - How to run one instance of a c# WinForm application? - Stack Overflow](https://stackoverflow.com/questions/12340043/how-to-run-one-instance-of-a-c-sharp-winform-application)
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food
-
Hi, OriginalGriff. I didn't follow you. Could you explain better? Or give me an example? Thanks.
What part doesn't make sense?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
-
Hi, folks. I’m developing an application in C# using Visual Studio. This application has a parent form from which the user can call other forms. My original problem was: if the user has already launched a form and he clicks again in the menu item to launch the same form, the program should only focus on the form. If the form is not on screen, the program should launch it. Another problem is that I have 1 form that is launched with 2 different data sets (different tables in a DB). I solved this problem this way: I declared 2 variables for the forms and defined 2 routines:
private void M2_SaidaEfetiva_Click(object sender, EventArgs e)
{
if (FormEfetivas == null)
{
FormEfetivas = new Frm_Saida();
FormEfetivas.MdiParent = this;
FormEfetivas.Text = "Saídas Efetivas";
FormEfetivas.Tag = "1";
FormEfetivas.Show();
}
FormEfetivas.Activate();
}
private void M2_SaidaPrevisao_Click(object sender, EventArgs e)
{
if (FormPrevisao == null)
{
FormPrevisao = new Frm_Saida();
FormPrevisao.MdiParent = this;
FormPrevisao.Text = "Previsão de Saídas";
FormPrevisao.Tag = "0";
FormPrevisao.Show();
}Now the problem is: when the user opens one form, then closes it, he cannot reopen it again, because the variables (FormEfetivas and FormPrevisao) are not eliminated when the user closes the form. What am I forgetting here? Can anybody help me? Thanks.
Simple enough:
private void M2_SaidaEfetiva_Click(object sender, EventArgs e)
{
var theForm = Application.OpenForms.OfType<Frm_Saida>().FirstOrDefault();
if (theForm is null)
{
theForm = new Frm_Saida();
theForm.MdiParent = this;
theForm.Text = "Saídas Efetivas";
theForm.Tag = "1";
theForm.Show();
}theForm.Activate();
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
What part doesn't make sense?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
It's not a matter of making sense. I understand what you wrote, but I'm confused about how to do it. Could you explain it? Thanks.
-
It's not a matter of making sense. I understand what you wrote, but I'm confused about how to do it. Could you explain it? Thanks.
Explain what? I have no idea which bits you are capable of doing for yourself! So ... you know how to add an event handler, yes?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!