如果 child 只是表单的副本,如何检测 MDIChild?
How to detect MDIChild if the child is just a duplicate of a form?
我有frm_Child
和frm_Parent
,我把frm_Parent
的isMDIContainer
设为true
在 frm_Parent 我有 2 个按钮
btn_Create
- 创建另一个 mdi child
btn_Detect
- 检测活动 mdi Child
(When I say active it means that I set my focused on it)
frm_Parent代码:
Public Class frm_Parent
Public my_variable As String
Private Sub btn_create_Click(sender As Object, e As EventArgs) Handles btn_create.Click
my_variable += 1
Call New frm_Child() With {.MdiParent = Me}.Show()
End Sub
Private Sub btn_Detect_Click(sender As Object, e As EventArgs) Handles btn_Detect.Click
'Code to detect active mdi child
End Sub
End Class
然后我有 frm_Child,其中我有 label1.text
,label1.text
的值将自动获得 form_load
,这完全取决于 my_variable
在 frm_Parent
frm_Child代码:
Public Class frm_Child
Private Sub frm_Child_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label1.Text = frm_Parent.my_variable
End Sub
End Class
示例屏幕截图,如果我创建 4 个新 frm_Child
问题: 如何以编程方式检测活动的 mdi child 并将 label1.text 显示到消息框? (note: active mean that I set my on it.)
我试过这样的东西,
Private Sub btn_Detect_Click(sender As Object, e As EventArgs) Handles btn_Detect.Click
'Code to detect active mdi child
Dim activeChild As Form = me.ActiveMdiChild
MsgBox(activeChild.label1.text)
End Sub
还有这个,我修改了the example from MSDN
Private Sub btn_Detect_Click(sender As Object, e As EventArgs) Handles btn_Detect.Click
'Code to detect active mdi child
Dim activeChild As Form = Me.ActiveMdiChild
' If there is an active child form, find the active control, which
' in this example should be a RichTextBox.
If (Not activeChild Is Nothing) Then
Try
Dim theLabel As Label = CType(activeChild.ActiveControl, Label)
MsgBox(theLabel.Text)
Catch
MessageBox.Show("You need to select an active form.")
End Try
End If
End Sub
因此,要总结答案中的评论,您需要通过父级的 ActiveMdiChild
属性 获取子表单并将其转换为适当的类型,以便能够访问该类型的成员。
就我个人而言,我认为您应该将 "my_variable" 值传递给 child 表单的 Constructor,但要遵循 jmcilhinney 的建议 casting,你应该这样抓取它(而不是使用默认实例):
Private Sub frm_Child_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim parent As frm_Parent = DirectCast(Me.MdiParent, frm_Parent)
Me.Label1.Text = parent.my_variable
End Sub
同样,下面是您如何投射 MdiChild 并在 "detect" 按钮中获取标签:
Private Sub btn_Detect_Click(sender As Object, e As EventArgs) Handles btn_Detect.Click
If Not IsNothing(Me.ActiveMdiChild) Then
Dim child As frm_Child = DirectCast(Me.ActiveMdiChild, frm_Child)
MessageBox.Show(child.Label1.Text)
End If
End Sub
当然,如果您要使用不同 类型 的 MdiChildren,那么您要么必须进行额外检查并转换为正确的类型,要么使用一个 Interface,所有 children 实现并转换为该接口。
我有frm_Child
和frm_Parent
,我把frm_Parent
的isMDIContainer
设为true
在 frm_Parent 我有 2 个按钮
btn_Create
- 创建另一个mdi child
btn_Detect
- 检测活动mdi Child
(When I say active it means that I set my focused on it)
frm_Parent代码:
Public Class frm_Parent
Public my_variable As String
Private Sub btn_create_Click(sender As Object, e As EventArgs) Handles btn_create.Click
my_variable += 1
Call New frm_Child() With {.MdiParent = Me}.Show()
End Sub
Private Sub btn_Detect_Click(sender As Object, e As EventArgs) Handles btn_Detect.Click
'Code to detect active mdi child
End Sub
End Class
然后我有 frm_Child,其中我有 label1.text
,label1.text
的值将自动获得 form_load
,这完全取决于 my_variable
在 frm_Parent
frm_Child代码:
Public Class frm_Child
Private Sub frm_Child_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label1.Text = frm_Parent.my_variable
End Sub
End Class
示例屏幕截图,如果我创建 4 个新 frm_Child
问题: 如何以编程方式检测活动的 mdi child 并将 label1.text 显示到消息框? (note: active mean that I set my on it.)
我试过这样的东西,
Private Sub btn_Detect_Click(sender As Object, e As EventArgs) Handles btn_Detect.Click
'Code to detect active mdi child
Dim activeChild As Form = me.ActiveMdiChild
MsgBox(activeChild.label1.text)
End Sub
还有这个,我修改了the example from MSDN
Private Sub btn_Detect_Click(sender As Object, e As EventArgs) Handles btn_Detect.Click
'Code to detect active mdi child
Dim activeChild As Form = Me.ActiveMdiChild
' If there is an active child form, find the active control, which
' in this example should be a RichTextBox.
If (Not activeChild Is Nothing) Then
Try
Dim theLabel As Label = CType(activeChild.ActiveControl, Label)
MsgBox(theLabel.Text)
Catch
MessageBox.Show("You need to select an active form.")
End Try
End If
End Sub
因此,要总结答案中的评论,您需要通过父级的 ActiveMdiChild
属性 获取子表单并将其转换为适当的类型,以便能够访问该类型的成员。
就我个人而言,我认为您应该将 "my_variable" 值传递给 child 表单的 Constructor,但要遵循 jmcilhinney 的建议 casting,你应该这样抓取它(而不是使用默认实例):
Private Sub frm_Child_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim parent As frm_Parent = DirectCast(Me.MdiParent, frm_Parent)
Me.Label1.Text = parent.my_variable
End Sub
同样,下面是您如何投射 MdiChild 并在 "detect" 按钮中获取标签:
Private Sub btn_Detect_Click(sender As Object, e As EventArgs) Handles btn_Detect.Click
If Not IsNothing(Me.ActiveMdiChild) Then
Dim child As frm_Child = DirectCast(Me.ActiveMdiChild, frm_Child)
MessageBox.Show(child.Label1.Text)
End If
End Sub
当然,如果您要使用不同 类型 的 MdiChildren,那么您要么必须进行额外检查并转换为正确的类型,要么使用一个 Interface,所有 children 实现并转换为该接口。