Solved

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

Posted on 2004-09-08
12
461 Views
Last Modified: 2008-01-16
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
0
Comment
Question by:acheaney
12 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 12008216
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
0
 

Expert Comment

by:Ilianam
ID: 12018198
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.
0
 

Author Comment

by:acheaney
ID: 12035596
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?
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 12035716
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
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 12035725
Lol...the problem appears to be in the way the OS handles MDI children.
http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_10072028.html
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 12035806
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
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 12036007
I think the problem is that when you switch windows, a WM_MDIACTIVATE message is sent to both the window losing focus and the window receiving focus:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/WinUI/WindowsUserInterface/Windowing/MultipleDocumentInterface/MultipleDocumentInterfaceReference/MultipleDocumentInterfaceMessages/WM_MDIACTIVATE.asp

Again, I think the only way to really solve the problem is subclassing the MDI child and/or MDI parent to either change or consume the MDI messages or the WM_SIZING message.

At any rate, it will be messy.

Idle_Mind
0
 

Expert Comment

by:Ilianam
ID: 12036009
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
0
 
LVL 8

Assisted Solution

by:wguerram
wguerram earned 250 total points
ID: 12044298
Check this:

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21098926.html

The accepted answer checks if the form is already open, if so just activate it.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Regex validation 2 28
VB.NET - Error - Object not set in Module 4 41
Import Data from Multiple Text Files in Excel 12 63
Error message when using PrintSocket.Send 7 19
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question