How to prevent multiple child forms opening

T

Tony K

I have a MDI application. On the menu toolstrip child forms are selected
from one of the menus. I don't want to play the disable/enable menu item
game. I have selected that open forms are added to the "Window" menu item.
What I'm looking for is when the user clicks on the menu item to open the
form, how can I check to see if it's open already and if so then set the
focus (if possible).

What happens now is if the menu item is clicked on 2, 3 or even 4 times, it
opens new instances of the form each time.

What can I change in this code that opens my child forms to prevent it from
opening new instances?

Dim frmCustomers As New Customers()
frmCustomers.MdiParent = Me
frmCustomers.WindowState = FormWindowState.Maximized
frmCustomers.Show()


Thanks,
Tony
 
H

Heikki Leivo

What can I change in this code that opens my child forms to prevent it
from opening new instances?

Dim frmCustomers As New Customers()
frmCustomers.MdiParent = Me
frmCustomers.WindowState = FormWindowState.Maximized
frmCustomers.Show()

You could, for example, use a variable to hold the form instance. For
example,

Private frmCustomers As Customers

Private Sub mnuShowCustomers_Click()
If frmCustomers Is Nothing OrElse frmCustomers.IsDisposed Then
frmCustomers = New Customers()
frmCustomers.MdiParent = Me
End If
frmCustomers.WindowState = FormWindowState.Maximized
frmCustomers.Show()
End Sub
 
T

Tony K

Thank you, I will try it today.

Tony


Heikki Leivo said:
You could, for example, use a variable to hold the form instance. For
example,

Private frmCustomers As Customers

Private Sub mnuShowCustomers_Click()
If frmCustomers Is Nothing OrElse frmCustomers.IsDisposed Then
frmCustomers = New Customers()
frmCustomers.MdiParent = Me
End If
frmCustomers.WindowState = FormWindowState.Maximized
frmCustomers.Show()
End Sub
 
T

Tony K

Thank you Cor. You've helped me out several times and I really appreciate
it. Thanks for the useful links.

Tony
 
S

Stephen Flynn

I use the following code. It will check each open forms type, if it finds
an open form of that type, then it will bring it to the front. If not
found, then it will open it.

For Each OpenForm As Form In Me.MdiChildren
If TypeOf OpenForm Is [FormName] Then
OpenForm.Activate()
Exit Sub
End If
Next
Dim NewForm As New [FormName]
NewForm.Visible = True
 
B

Branco Medeiros

Tony said:
I have a MDI application. On the menu toolstrip child forms are selected
from one of the menus. I don't want to play the disable/enable menu item
game. I have selected that open forms are added to the "Window" menu item.
What I'm looking for is when the user clicks on the menu item to open the
form, how can I check to see if it's open already and if so then set the
focus (if possible).
What can I change in this code that opens my child forms to prevent it from
opening new instances?

Dim frmCustomers As New Customers()
frmCustomers.MdiParent = Me
frmCustomers.WindowState = FormWindowState.Maximized
frmCustomers.Show()

If you're using VB 2005 you can use the My.Forms collection:

frmCustomers.MdiParent = Me
frmCustomers.WindowState = FormWindowState.Maximized
frmCustomers.Show()
frmCustomers.Activate
</aircode>

This will show the Customers form if it's not shown already (or was
closed) and give it focus if it's already shown.

HTH.

Regards,

Branco.
 
L

lord.zoltar

Y'know what else works? Open the form by the name of it's CLASS.

Example: If the form's class's name is "MyForm1" and you do
"MyForm1.Show", no matter how many times you click the button that
executes "MyForm1.Show", the form will only open once. I don't like
this one too much myself, but it works in a pinch, and is pretty easy
to implement.
 
C

Cyril Gupta

Hello Tony,

Here's an alternative method

If frmCustomers Is Nothing OrElse frmCustomers.IsDisposed Then
frmCustomers = New Customers()
End If
frmCustomers.MdiParent = Me
frmCustomers.WindowState = FormWindowState.Maximized
frmCustomers.Show()


-- You can do anything with a little bit of 'magination.

-- I've been programming so long, my brain is now software.
 
B

Branco Medeiros

Cor said:
In all versions is the MDIChildren collection

http://msdn2.microsoft.com/en-us/library/system.windows.forms.form.md...

Because of your message I got the impression that you did not know that.
<snip>

Yes, but the MDIChildren collection lists all *open* child forms. That
would mean look up the child in the MDIChildren, and create a new
instance if it wasn't there... more trouble for what the OP was
asking, I guess.

Regards,

Branco.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top