Link to home
Start Free TrialLog in
Avatar of acheaney
acheaney

asked on

MDI - Prevent Reuse Child windows or Prevent Multiple instances of a child window.

Basically what I am trying to do is reuse my child windows.
Take a look at the Outlook 2003 interface, and I'm basically trying to mimic this using MDI.

If the MDI Child is already open, I would like to just display the current one instead of opening another.

How is this possible?
I'm in a pinch for time, so big points for this one!
Thanks
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Here is one way:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Not loadMDIChild("Form2") Then
            Dim f As New Form2
            f.Name = "Form2"
            f.MdiParent = Me
            f.Show()
        End If
    End Sub

    Private Function loadMDIChild(ByVal childName As String) As Boolean
        Dim f As Form
        For Each f In Me.MdiChildren
            If f.Name.Equals(childName) Then
                If f.WindowState = FormWindowState.Minimized Then
                    f.WindowState = FormWindowState.Normal
                End If
                f.Focus()
                Return True
            End If
        Next
        Return False
    End Function
Avatar of Ilianam
Ilianam

I make a class like this

Public Class clsGlobals
    'I make a class with Public Shared variables with the type as the Form itself.
#Region "Singleton Forms"
    Public Shared gfrmSearch As frmSearch
    Public Shared gfrmContact As frmContact
#End Region
 
Then  from my menu:
Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click

        'to open the form I use the following...
        'I use the focus to ensure it is always in the foreground
        'this can be important in MDI application
        'this is also what enforces singleton
        If clsGlobals.gfrmSearch Is Nothing Then
            clsGlobals.gfrmSearch = New frmSearch
        End If

        clsGlobals.gfrmSearch.MdiParent = Me
        'if it isn't nothing then the Show() method will still bring it up
        clsGlobals.gfrmSearch.Show()
        clsGlobals.gfrmSearch.Focus()

    End Sub
End Class
and in each form
End Class

Then
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'to open the form I use the following...
        'I use the focus to ensure it is always in the foreground
        'this can be important in MDI application
        'this is also what enforces singleton
        If clsGlobals.gfrmContact Is Nothing Then
            clsGlobals.gfrmContact = New frmContact
        End If

        clsGlobals.gfrmContact.MdiParent = Me.MdiParent

        'if it isn't nothing then the Show() method will still bring it up
        clsGlobals.gfrmContact.Show()
        clsGlobals.gfrmContact.Focus()
    End Sub

IMPORTANT

   clsGlobals.gfrmSearch = Nothing 'set the variable to nothing for future searches if window is closed.
Avatar of acheaney

ASKER

With either of these, as I switch between windows, the sizes of them keep changing.
All the windows should stay maximized, but they keep changing.

They also flicker the previously opened windows before changing.
Can this be hidden?
Interesting behaviour...

When the child windows are maximized and you switch to another child window, the current child windowstate is reset to normal before the child being switched to is brought up.

Still tyring to find a solution to the problem...

Idle_Mind
Lol...the problem appears to be in the way the OS handles MDI children.
https://www.experts-exchange.com/questions/10072028/MDI-children.html
I am finding many, many posts here on EE dealing with this exact problem in several of the TAs.  The language doesn't seem to matter as this problem is a result of the way windows deals with MDI children.  The dates of these questions go all the way back to the earliest days of EE in the late 90's.

Most of the solutions solve the problem by not using borders on the children forms.  Some make the normal size of the form the same as the maximized size no matter what.

I couldn't find one solution that REALLY solved the problem elegantly though.

I'm thinking it would be possible to subclass the form whenever it is maximized and change the MINMAXINFO so that the minimum size is always the same as the maximized size.  Still trying to figure out how to determine when the form should be restored back to a normal state though.

Working on it...

Idle_Mind
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America 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
Helllo,
But the whole idea of the very impotant part is that you copy that in the dispose event for every form.
In my example I only want to have ONE CHIld, like I select a form1, and that one opens form2, if I make changes In form2 and need to go back to form1 I will have the same results because this child exist
SOLUTION
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