borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (End of WorkSheet)Next Topic (*urgent! help needed here!) New Topic New Poll Post Reply
AndreaVB Forum : VB.Net : Monitor MDIChildren
Poster Message
MikeG
Level: Sage

Registered: 21-02-2003
Posts: 54

icon Monitor MDIChildren

Using VB 2005, I'd like to dynamically populate a pulldown menu called "Windows" within an MDI form from all currently open MDI Child windows.  When another MDI Child opens, it automatically gets added to the Windows pulldown and when one closes it automatically gets removed from the pulldown window.

Anyone done this before?

Thanks!

19-01-2007 at 07:47 PM
View Profile Send Email to User Show All Posts | Quote Reply
MikeG
Level: Sage

Registered: 21-02-2003
Posts: 54
icon Re: Monitor MDIChildren

Ok, I finally came up with a sollution.  The only difference is that I populate the window pulldown list in the DropDownOpening event in the menu object (in my code it's called mnuWindowsOpen).  Therefore, the dropdown list gets cleared and repopulated each time the menu is selected.  This was necessary to take into account the potential changing of the form text (MDIChild title) in my application.  I also created a class called clsListWin to keep track of all the information I need.

I'm sure there's a better way to do this... but it works!

If anyone's interested, here's the code I put in the main form (MDI Parent)


    Public WindowList As New System.Collections.Generic.List(Of clsListWin)

    '--- Default, Returns the index of an existing window.  If ReturnIndex = False then return a count of matching windows ---
    Private Function isInWindowsList(ByVal WindowName As String, Optional ByVal ReturnIndex As Boolean = True) As Integer
        Dim i As Integer
        Dim cnt As Integer = 0
        Dim parenPos1 As Integer
        Dim parenPos2 As Integer
        Dim checkName1 As String
        Dim checkName2 As String

        parenPos1 = InStr(WindowName, "(")
        If parenPos1 > 0 Then
            checkName1 = Mid(WindowName, 1, parenPos1 - 1)
        Else
            checkName1 = Mid(WindowName, 1)
        End If

        For i = 0 To mnuWindowsOpen.DropDownItems.Count - 1
            parenPos2 = InStr(mnuWindowsOpen.DropDownItems(i).ToString, "(")

            If parenPos2 > 0 Then
                checkName2 = Mid(mnuWindowsOpen.DropDownItems(i).ToString, 1, parenPos2 - 1)
            Else
                checkName2 = Mid(mnuWindowsOpen.DropDownItems(i).ToString, 1)
            End If

            If checkName1 = checkName2 Then
                If ReturnIndex = True Then
                    Return i
                Else
                    cnt += 1
                End If
            End If
        Next
        Return IIf(ReturnIndex = True, -1, cnt)
    End Function

    Private Sub showWindow(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim wn As clsListWin

        For Each wn In WindowList
            If wn.WinName = sender.ToString Then
                wn.FormObject.Select()
                If wn.FormObject.WindowState = FormWindowState.Minimized Then
                    wn.FormObject.WindowState = FormWindowState.Normal
                End If
            End If
        Next

    End Sub

    Public Sub buildWindowList()
        Dim frmAry As Form()
        Dim eHndlr As System.EventHandler = AddressOf showWindow
        Dim i As Integer
        Dim Index As Integer
        Dim winName As String
        Dim toolTip As String
        Dim cnt As Integer

        '--- clear existing items in dropdown ---
        mnuWindowsOpen.DropDownItems.Clear()
        '--- clear existing items in class ---
        WindowList.Clear()

        '--- Get array of Child forms ---
        frmAry = Me.MdiChildren
        If Me.MdiChildren.GetLength(0) < 1 Then Exit Sub

        If IsNothing(Me.ActiveMdiChild) Then Exit Sub

        For i = 0 To Me.MdiChildren.GetLength(0) - 1
            winName = frmAry(i).Text
            toolTip = winName

            cnt = isInWindowsList(winName, False)
            If cnt > 0 Then winName = winName + "(" + (cnt + 1).ToString + ")"

            Index = mnuWindowsOpen.DropDownItems.IndexOf(mnuWindowsOpen.DropDownItems.Add(winName, mnuWindowsOpen.BackgroundImage, eHndlr))

            mnuWindowsOpen.DropDownItems(Index).ToolTipText = toolTip
            WindowList.Add(New clsListWin(winName, frmAry(i).Name, frmAry(i).Text, Index, toolTip, frmAry(i)))
        Next

    End Sub

    Private Sub mnuWindowsOpen_DropDownOpening(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuWindowsOpen.DropDownOpening
        buildWindowList()
    End Sub


and here's the simple class i created to keep track of the Windows/MDIChildren

Option Explicit On
'Option Strict On
Imports System.Collections

Public Class clsListWin
    Private WinName_value As String
    Private FormName_value As String
    Private FormText_value As String
    Private index_value As Integer
    Private ToolTipText_value As String
    Private FormObject_value As Form

    Public Sub New(ByVal WinName As String, ByVal FormName As String, ByVal FormText As String, ByVal index As Integer, ByVal ToolTipText As String, ByVal FormObject As Form)
        WinName_value = WinName
        FormName_value = FormName
        FormText_value = FormText
        index_value = index
        ToolTipText_value = ToolTipText
        FormObject_value = FormObject
    End Sub


    Public Property WinName() As String
        Get
            Return WinName_value
        End Get
        Set(ByVal value As String)
            WinName_value = value
        End Set
    End Property

    Public Property FormName() As String
        Get
            Return FormName_value
        End Get
        Set(ByVal value As String)
            FormName_value = value
        End Set
    End Property

    Public Property FormText() As String
        Get
            Return FormText_value
        End Get
        Set(ByVal value As String)
            FormText_value = value
        End Set
    End Property

    Public Property ToolTipText() As String
        Get
            Return ToolTipText_value
        End Get
        Set(ByVal value As String)
            ToolTipText_value = value
        End Set
    End Property

    Public Property index() As Integer
        Get
            Return index_value
        End Get
        Set(ByVal value As Integer)
            index_value = value
        End Set
    End Property

    Public Property FormObject() As Form
        Get
            Return FormObject_value
        End Get
        Set(ByVal value As Form)
            FormObject_value = value
        End Set
    End Property

End Class





[Edited by MikeG on 02-02-2007 at 08:31 AM GMT]

01-02-2007 at 03:38 PM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : VB.Net : Monitor MDIChildren
Previous Topic (End of WorkSheet)Next Topic (*urgent! help needed here!) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder