【问题标题】:List of Windows in vb.net applicationvb.net 应用程序中的 Windows 列表
【发布时间】:2014-10-08 22:32:35
【问题描述】:

我有一个 MDI 应用程序,我正在尝试获取 ComponentOne 功能区菜单的打开窗口列表。使用 VB .NET。 我有这个子用于在 ​​MDI 容器中实例化一个新的子窗体:

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

然后在功能区菜单的另一个 Sub 中,我尝试获取窗口列表。 我试过这个:

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

但是我在System.Windows.Window 集合上得到了一个NullReferenceException。 所以我尝试了这个:

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

但是,在新的RibbonButton 的参数上,我得到“重载解析失败,因为在没有缩小转换的情况下无法调用可访问的'new'”。如果我打开Option Strict,当然它会说它不允许后期绑定。

所以我想最终我想知道为什么我的 Windows 集合是空的,即使我已经打开了子表单。 那么,除此之外,为什么 New RibbonButton 接受 frm.Title 而不是 Window.Title。

注意(如果您想知道的话)... frmButton 是一个类对象:

Friend WithEvents frmButton As C1.Win.C1Ribbon.RibbonButton

谢谢!

【问题讨论】:

  • Winforms 和 WPF 代码的奇怪组合。使用父级的 MdiChildren 属性。摆脱 WPF 引用,它们会给你带来麻烦。
  • OK - 改用 MdiChildren 有效
  • 您指的是哪些 WPF 引用?
  • 他指的是 Dim frm As System.Windows.Window 的 WinForms 应该是 System.Windows.Forms.Form 或只是 Form 因为你不应该添加所有的命名空间

标签: vb.net mdi componentone


【解决方案1】:

感谢来自多个来源的线索,我能够让它工作。如果其他人想知道如何,这是我的示例代码:

公共类主窗体

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

结束类

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多