Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MDI Form and only one instance of Child forms

Status
Not open for further replies.

adalli

Programmer
Feb 8, 2005
44
MT
Hi,

I have a MDI form with a menu structure. Each menu item opens a form using the following code.

Dim myForm As New frmClients
myForm.ShowDialog()
myForm = Nothing

The above code does not allow the user to go back to the MDI Menu unless he closes the current form. On the other hand if I use the code myForm.show, everytime the users goes to the menu, a new instance of the same form is opened.

Is there a way to open a new form when the form is not already displayed, and leaving the MDI Form available throughout but if an instance of a form is already opened, that instance would be displayed?

Thanks in advance,
 
Hi,

You can do:
Dim myForm As New frmClients
myForm.ShowDialog()
myForm = Nothing
<menuName>.enabled=false

And at closing of the form (event handler) do: <menuName>.enabled=true

I mean that you should disable the option to load a new form, and enable it back when the form is closed. This is a good way if you want to have only 1 instance of some mdiChild forms.
 
Here is another way..(tested on vb2005 express)
Code:
    Private WithEvents F2 As Form2

'==========================================
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        '  If the instance still exists... (ie. it's Not Nothing)
        If Not IsNothing(F2) Then
            '  and if it hasn't been disposed yet
            If Not F2.IsDisposed Then
                '  then it must already be instantiated - maybe it's
                '  minimized or hidden behind other forms ?
                F2.WindowState = FormWindowState.Normal  ' Optional
                F2.BringToFront()  '  Optional
            Else
                '  else it has already been disposed, so you can
                '  instantiate a new form and show it
                F2 = New Form2()
                F2.Show()
            End If
        Else
            '  else the form = nothing, so you can safely
            '  instantiate a new form and show it
            F2 = New Form2()
            F2.Show()
        End If

        If IsNothing(F2) Then MessageBox.Show("Is Nothing")
        If F2.IsDisposed Then MessageBox.Show("Is Disposed")


    End Sub

________________________________________________________
Zameer Abdulla
Help to find Missing people
Even a thief takes ten years to learn his trade.
 
That did it. Excellent, many thanks for your help.
 
of course you should try the OO way and make your class(form) a singleton. Not so hard and lots of examples o be found on google.

Christiaan Baes
Belgium

"Time for a new sig." - Me
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top