Link to home
Start Free TrialLog in
Avatar of Ilianam
Ilianam

asked on

MDI Childrens or Singleton

Hello,
I have a vb.net application. But I only want to have 1 actife children.....
This is the issue.
I have a search option in my main menu, which works fine. However, if I select search again, it will open a new instance (child) instead of refreshing the previous one....
Please help
Avatar of SRigney
SRigney
Flag of United States of America image

You should be able to check to see if it already exists, and if it does switch to it instead of creating a new one each time.

In your main you can do something like this

Dim frmSearch As SearchForm

Then in your menu item click event

If frmSearch != null Then
   frmSearch.Show()
Else
   frmSearch = New SearchForm
End If

That should be close enough to give you the idea.
Avatar of Mike Tomlinson
SRigney is right on track.  Here is a more complete example (assuming Form1 is the MDIParent and Form2 is the MDIChild):

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private WithEvents f2 As Form2

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If f2 Is Nothing Then
            f2 = New Form2
            f2.MdiParent = Me
            f2.Show()
        End If

        If f2.WindowState = FormWindowState.Minimized Then
            f2.WindowState = FormWindowState.Normal
        End If
        f2.Focus()
    End Sub

    Private Sub f2_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles f2.Closed
        f2 = Nothing
    End Sub

End Class
Avatar of Ilianam
Ilianam

ASKER

Is this for vb.net?
Avatar of Ilianam

ASKER

SGrigney It doesn't work
You can use this method:

    Private Function IsChildInMemory(ByVal pChildName As String) As Boolean
        'A small method that checks to see if a particular form is currently displayed as a child form
        Dim frmChild As Form
        For Each frmChild In Me.MdiChildren
            If TypeName(frmChild).ToUpper = pChildName.ToUpper Then
                frmChild.Activate()
                Return True
            End If
        Next
        Return False
    End Function
Avatar of Ilianam

ASKER

Emoreau,
It works, however if I "search" again its minimized...The main menu is an mdicontainer at run time....
>>however if I "search" again its minimized

What is minimizing your child form? You could check the windowstate to see if it is minimize!
Look at my example...it shows how to check the windowstate and put it back to normal...

Idle_Mind
Avatar of Ilianam

ASKER

Thank you...But I tried everything and I still cann't see my child.....
To further troubleshoot your problem we would need to see your code.

Idle_Mind
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ilianam

ASKER

Thank you to all the solution was pretty simple
Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
        If Me.MdiChildren.GetLength(0) < 2 Then
            Dim Look As New Search
            Look.MdiParent = Me
            Look.Text = "Search " & Me.MdiChildren.GetLength(0)
            Look.Show()
       end if
Avatar of Ilianam

ASKER

However is there a list of mdi properties that you can change at run time?