Reputation: 8071
I have window "Child 1", it opens from "Parent". Once I click in menu to open "Child 1" it can open several windows if I click several times in the menu. How to verify if the window "Child 1" opens then we should just bring it up.
The code which I use to open the window:
var ticketTypesForm = new fTicketTypes();
ticketTypesForm.Show();
Upvotes: 0
Views: 1453
Reputation: 11235
If it is ok for your app to use the child window in Modal mode (you don't need user interaction with parent window ) then just use the child one as Modal. The window is always on top.
var ticketTypesForm = new fTicketTypes(); ticketTypesForm.ShowDialog(this);
Also your app can close the child window by method Hide() when user closes the window. So the dialog will be never disposed. But in this case you must use the same instance of ticketTypesForm (not creating the new one every time the window is opened )
init app or first displaying
var ticketTypesForm = new fTicketTypes();
show
ticketTypesForm.Show(this);
close
ticketTypesForm.Hide();
Upvotes: 0
Reputation: 2292
Before show your new form again, check if its already opened or not using :
Application.OpenForms.OfType<YOUR_FORM_TYPE>().Any())
and if its opened, ignore he command, but if not open it again, you can do the following :
ticketTypesForm myTicketTypesForm;
private void OpenDialog(object sender, EventArgs e)
{
if (!Application.OpenForms.OfType<ticketTypesForm>().Any())
{
if (myTicketTypesForm == null)
myTicketTypesForm = new ticketTypesForm();
myTicketTypesForm.Show();
}
else
{
myTicketTypesForm.Focus();
}
}
Upvotes: 1
Reputation: 1275
Calling the Application.OpenForms
will give you a collection of all your open forms. You can just navigate through each of the open forms to check if Child Form 1 has been created already. If it is just call the .Focus()
method to bring it up front. If it has not been created yet, create the form as you would.
Upvotes: 3
Reputation: 778
Don't use var, instead you can do this
fTicketTypes ticketTypeForm;
//Some code goes here.
if(ticketTypeForm == null)
ticketTypeForm = new fTicketTypes();
ticketTypeForm.Show();
Upvotes: 1
Reputation: 112
Just keep a reference from your class instead creating one everytime.
Upvotes: 1