【问题标题】:Access MDI Parent StatusStrip in Child Window VB.NET 2015在子窗口 VB.NET 2015 中访问 MDI 父 StatusStrip
【发布时间】:2020-09-24 13:16:27
【问题描述】:

VB.NET 2015 -- 在 MDI 窗口中放置了带有 2 个 ToolStripStatusLabel 的 StatusStrip 控件。当我打开一个新表单时,这将是 MDI 窗口的子窗口。

我如何从信息 ToolStripStatusLabel 中读取。

For Each 不会遍历 StatusStrip 中的所有项目,据我所知,ToolStripStatusLabel 本身不是一个控件,因此 For each 循环不起作用。

Me.Owner.Controls 也无法访问 ToolStripStatusLabel,原因同上

做什么?

【问题讨论】:

  • 你为什么要使用Me.Owner?首先,您是如何使孩子形成孩子形式的?您设置了MdiParent 属性,对吗?所以很明显,您使用Me.MdiParent 来访问MDI 父级。您可以将其转换为其实际类型(而不仅仅是Form),然后访问其所有成员,包括StatusStrip
  • 也就是说,这并不是真正合适的方法。理想情况下,如果子表单需要来自父表单的数据,它会引发一个事件。然后父表单处理该事件并设置e 参数的属性,然后子表单可以读取该属性。这样,子表单就不必知道有关父表单的任何具体信息。此外,数据不应来自StatusStrip。父表单应该将用于设置标签文本的原始数据存储在某处,并且这些数据应该传递给子表单。我会发布这个效果的答案。

标签: vb.net


【解决方案1】:

任何子窗体从其父窗体获取数据的正确方法是子窗体引发事件,父窗体通过事件参数将数据传回给子窗体。这样,孩子就不必知道任何关于父表单的具体信息,因此理论上,许多不同的父表单可以显示相同的子表单并传递数据。这是一个松散耦合的例子。

子表单看起来像这样,提供一个在需要数据时引发的事件,并通过它创建的自定义 EventArgs 对象获取该数据:

Public Class Form2

    Public Event StatusDataNeeded As EventHandler(Of StatusDataNeededEventArgs)

    Protected Overridable Sub OnStatusDataNeeded(e As StatusDataNeededEventArgs)
        RaiseEvent StatusDataNeeded(Me, e)
    End Sub

    Private Sub GetStatusData()
        Dim e As New StatusDataNeededEventArgs

        OnStatusDataNeeded(e)

        MessageBox.Show(e.StatusData)
    End Sub

    '...

End Class


Public Class StatusDataNeededEventArgs

    Public Property StatusData As String

End Class

如您所见,那里不依赖任何特定类型的父窗体,也不依赖此窗体是 MDI 子窗体。它只是引发它的事件,任何监听的人都可以提供状态数据,无论关系如何。

在您的情况下,MDI 父窗体可能如下所示:

Public Class Form1

    'Stores the data that will be displayed in the StatusStrip.
    Private statusData As String

    'Display the status data in the StatusStrip.
    Private Sub SetStatusText()
        ToolStripStatusLabel1.Text = statusData
    End Sub

    'Create and display a child form.
    Private Sub DisplayChildForm()
        Dim childForm As New Form2 With {.MdiParent = Me}

        'Handle the event raised when the child requires the status data.
        AddHandler childForm.StatusDataNeeded, AddressOf ChildForm_StatusDataNeeded

        childForm.Show()
    End Sub

    'Pass the status data to the child form.
    Private Sub ChildForm_StatusDataNeeded(sender As Object, e As StatusDataNeededEventArgs)
        e.StatusData = statusData
    End Sub

    '...

End Class

如您所见,状态数据存储在其自己的字段中。 StatusStrip 用于显示状态数据,而不是存储。父窗体创建并显示一个子窗体并处理事件。在事件处理程序中,它只是将状态数据传递给e 参数的属性。

要了解有关创建自己的事件的更多信息,请参阅here

【讨论】:

  • 感谢您快速详细的回复,我会尝试并回复
【解决方案2】:

jmcilhinney 的 Event 方法绝对是一种更好的方法,但是作为一种快速解决方案,My 命名空间可以在 VB.Net 中使用。

My.Forms.<Form Name>.<Control name>.<property>

例如:

My.Forms.AM_MDI.ToolStripStatusLabel1.Text

对于那些喜欢阅读更多关于我的命名空间的人 - VB.NET 中的命名空间 https://www.thoughtco.com/namespaces-in-vbnet-3424445

您在 VB.NET 中使用“我的”命名空间吗? Do you use the 'My' namespace in VB.NET?

【讨论】:

  • 我实际上更喜欢这个和 MdiParent,而不是 Jim 仅在涉及 Control 时显示的 Event 方法。 Jim 经验丰富,知道最好将逻辑与表示层分开。这样他就可以在不模拟任何 GUI 控件的情况下测试逻辑。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-12
  • 1970-01-01
相关资源
最近更新 更多