Solved

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

Posted on 2004-09-08
12
446 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now