Solved

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

Posted on 2004-09-08
12
460 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with sorting data in Listbox using VB.NET 3 63
vb.net 2 44
Exit the loop 4 43
export data table to excel in vb.net 10 27
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

831 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