就像bodjo说的,你有[YourMDIForm] .ActiveMDIChild属性。
什么是不清楚,就像Plutonix说的是你的保存“按钮”在哪里。您的意思是:
第一种情况是非常简单的:使用的.ActiveMDIChild
你MDIParent表单。
的secund可能需要一些有效的方法来指向一个实际活动的子窗体(类似于你与你的globalVar
尝试一个...有更好的方法来做到这一点。
顺便说一句,当你得到你的。通过.ActiveMDIChild针对性MDIChild,你必须使用一个公共的朋友或受保护的变量通常,在窗体控件是私有的访问文本框所以,你可能必须:。
Public Class [YourMDIChildClassName]
' ...
' create a Public Property
Public ReadOnly Property ContentToSave() As String
Get
Return [YourTextBox].Text
End Get
End Property
' or make your textbox public in the Form design
Public [YourTextBox] As TextBox
' ... the one or the other, not both.
' ...
End Class
另一种方式来访问“活动” MDIChild ,假设您的所有MDIChild都是同一类的实例是在您的Child cl中创建一个静态(共享)属性屁股:
Public Class [YourMDIChildClassName]
' ...
Private Shared _CurrentMDIChild As [YourMDIChildClassName] = Nothing
Public Shared ReadOnly Property CurrentMDIChild() As [YourMDIChildClassName]
Get
Return _CurrentMDIChild
End Get
End Property
' ...
End Class
而且使用您是否尝试过同样的事情,但使用.Activated代替.Clicked
Public Class [YourMDIChildClassName]
' ...
Private Sub MyChildForm_Activated() Handles Me.Activated
_CurrenMDIChild = Me
End Sub
' And that's ALL this method SHOULD contain.
' If you try to add other activation tricks, like activating another Form,
' or firing up a DialogBox (worst case),
' everything will go WRONG and your application will hang in an endless loop !!!
' Be carefull when using .Activated.
' ...
End Class
然后,你可以使用静态属性访问当前活动MDIChild:
[YourMDIChildClassName].CurrentMDIChild
' => Gives you directly a VALID instance of your MDI Child (or Nothing !)
' Then you can use in your MDI Parent :
If [YourMDIChildClassName].CurrentMDIChild IsNot Nothing Then
Dim TextToSave As String = [YourMDIChildClassName].CurrentMDIChild.ContentToSave
' ... save your text...
End If
但是,因为你已经创建了一个静态(共享)指针指向最后一个活动的MDIChild (有点,我知道它不是C中的指针)无论何时关闭MDIChild窗体,您都必须更新此指针!
Public Class [YourMDIChildClassName]
' ...
Private Sub [YourMDIChildClassName]_FormClosing(_
sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
If _CurrentMDIChild Is Me Then ' And "Me" is closing right now...
_CurrentMDIChild = Nothing
' We don't want memory leak or pointer to a Form that has been closed !
End If
End Sub
' ...
End Class
我们不应该创建处理Child Form激活的自定义方式。 MDIParent.ActiveMDIChild
是那里的。但是,一旦我们想要访问扩展/定制/特定的子窗体的属性最初并不存在于System.Windows.Forms.Form
中,我们需要将MDIParent.ActiveMDIChild
转换为MDIChild的实际窗体派生类型。这是另一种方式来做到这一点,但它只是我:我不喜欢很多铸件。始终IDE设置为:
Option Explicit On
Option Strict On
Option Infer Off
Forms.Clicked
...没有这个事件(点击)真的存在吗?我知道.Click
但不是“点击”。无论如何:
Public Class [YourMDIChildClassName]
' ...
Private Sub MyChildForm_Clicked() Handles Me.Click
' This part is executed when you click INSIDE the Form,
' at a location where there is NO Control.
' If you click on a control, like a Textbox, a Picturebox, a Panel, etc
' this block IS NOT EXECUTED as you've clicked on a control,
' not on the Form !
End Sub
' ...
End Class
保存按钮驻留在哪里?每个表单上是否有一个? – Plutonix 2014-10-07 01:01:13