Andy
Andy

Reputation: 646

List of Windows in vb.net application

I have an MDI application where I'm trying to get a list of open windows for a ComponentOne Ribbon Menu. Using VB .NET. I have this sub for instantiating a new child form within the MDI container:

Private Sub newButton_Click(sender As Object, e As EventArgs) Handles newButton.Click
    ' Create a new instance of the child form.
    Dim ChildForm As New MyProject.MyForm
    'Make it a child of this MDI form before showing it.
    ChildForm.MdiParent = Me

    m_ChildFormNumber += 1
    ChildForm.Text = "Window " & m_ChildFormNumber

    ChildForm.Show()
End Sub

Then in another Sub for the ribbon menu I try to get the list of windows. I tried this:

    Dim frm As System.Windows.Window
    For Each frm In My.Application.Windows
        frmButton = New C1.Win.C1Ribbon.RibbonButton(frm.Title)
    ...

But I get a NullReferenceException on the System.Windows.Window collection. So then I tried this:

    For Each Window In My.Application.Windows
        frmButton = New C1.Win.C1Ribbon.RibbonButton(Window.Title)
    ...

But with that, I get "overload resolution failed because no accessible 'new' can be called without a narrowing conversion" on the arguments for the new RibbonButton. If I turn Option Strict On, of course it says it disallows late binding.

So I guess ultimately I'm wondering why my Windows collection is empty, even if I've opened child forms. Then even beyond that, why does the New RibbonButton accept frm.Title but not Window.Title.

NOTE (in case you were wondering)...the frmButton is a class object:

Friend WithEvents frmButton As C1.Win.C1Ribbon.RibbonButton

Thank you!

Upvotes: 1

Views: 3241

Answers (2)

Robin Bennett
Robin Bennett

Reputation: 3231

8 years later, this seems to be the only question on how to implement a Window menu, so here's my updated version for the standard MenuStrip control.

Friend WithEvents ChildWindowMenu As ToolStripMenuItem

Private Sub WindowMenu_DropDownOpening(sender As Object, e As EventArgs) Handles windowMenu.DropDownOpening
    Try

        windowMenu.DropDownItems.Clear()

        For Each child In Me.MdiChildren
            ChildWindowMenu = New ToolStripMenuItem With {
                .Text = child.Text,
                .Tag = child
            }
            If child Is ActiveMdiChild Then
                ChildWindowMenu.Checked = True
            End If
            windowMenu.DropDownItems.Add(ChildWindowMenu)
            AddHandler ChildWindowMenu.Click, AddressOf ChildWindowMenu_Click
        Next

    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

End Sub

Private Sub ChildWindowMenu_Click(sender As Object, e As EventArgs)
    Try
        CType(CType(sender, ToolStripMenuItem).Tag, Form).Activate()
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

Upvotes: 2

Andy
Andy

Reputation: 646

Thanks to clues from multiple sources, I was able to get it working. In case anyone else is wondering how, here's my sample code:

Public Class mainForm

Private m_ChildFormNumber As Integer
Friend WithEvents frmButton As C1.Win.C1Ribbon.RibbonButton

Private Sub newButton_Click(sender As Object, e As EventArgs) Handles newButton.Click
    ' Create a new instance of the child form.
    Dim ChildForm As New ProofOfConcept.FormResize
    'Make it a child of this MDI form before showing it.
    ChildForm.MdiParent = Me

    m_ChildFormNumber += 1
    ChildForm.Text = "Window " & m_ChildFormNumber

    ChildForm.Show()
End Sub

Private Sub windowMenu_Dropdown(sender As Object, e As EventArgs) Handles windowMenu.DropDown

    Dim count As Integer = Me.MdiChildren.Length
    windowMenu.Items.ClearAndDisposeItems()

    For i As Integer = 0 To count - 1
        frmButton = New C1.Win.C1Ribbon.RibbonButton
        frmButton.Text = Me.MdiChildren(i).Text
        frmButton.Tag = i
        If MdiChildren(i) Is ActiveMdiChild Then
            frmButton.SmallImage = My.Resources.test
        End If
        windowMenu.Items.Add(frmButton)
        AddHandler frmButton.Click, AddressOf frmButton_Click
    Next

End Sub

Private Sub frmButton_Click(sender As Object, e As EventArgs)

    Dim Rb As C1.Win.C1Ribbon.RibbonButton = DirectCast(sender, C1.Win.C1Ribbon.RibbonButton)
    Me.ActivateMdiChild(MdiChildren(CInt(Rb.Tag)))
    Me.MdiChildren(CInt(Rb.Tag)).Focus()

End Sub

End Class

Upvotes: 1

Related Questions