VB从选定的表单中获取文本

时间:2014-10-07 00:59:42

标签: vb.net textbox mdi

我想知道如何从所选表单中的文本框中获取一些文本。我有一个MDI表单,其中包含X个子表单。每个子表单中都有一个文本框,其中包含一些文本。当我点击保存按钮时,我如何知道选择了哪个表单以及如何从该文本框中获取文本。

Private Sub Forms_Clicked(sender As Object, e As EventArgs) Handles Forms.clicked
    globalVar = sender
End Sub

我正在考虑创建一个事件,只检测何时单击某个表单并将其表单/表单ID保存在全局变量中。有什么想法吗?

2 个答案:

答案 0 :(得分:1)

您必须先determine the active child from,然后在那里,您可以查看要阅读其内容的文本框。

答案 1 :(得分:0)

就像bodjo说的那样,你有[YourMDIForm] .ActiveMDIChild属性。

不清楚的是,像Plutonix所说的那样,你的Save" Button"。你的意思是:

  • 从MDI父表单上的MenuStrip中保存MenuItem?

  • 或MDI子表单中的保存按钮(或不属于MDI父/子系统的其他表单)

第一种情况非常简单:使用MDIParent表格的.ActiveMDIChild secund可能需要一些有效的方式来指向实际的活动子表单(类似于您使用globalVar尝试的表单...有更好的方法来执行此操作。

顺便说一句,当你通过.ActiveMDIChild获得目标MDIChild时,你必须使用Public,Friend或Protected变量访问文本框。通常,表单中的控件是私有的。所以你可能不得不:

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类中创建一个静态(共享)属性:

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

HOWEVER ,因为您已经创建了一个指向最后一个活动的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

我们不应该创建处理子表单激活的自定义方式。 MDIParent.ActiveMDIChild就是为此而存在的。但是,一旦我们想要访问System.Windows.Forms.Form中最初不存在的子表单的扩展/自定义/特定属性,我们需要将MDIParent.ActiveMDIChild强制转换为Option Explicit On Option Strict On Option Infer Off 我们的MDIChild的真实Form派生类型。这是另一种方法,但它只是我:我不喜欢铸件。始终将IDE设置为:

Forms.Clicked



.Click ...这个事件(Clicked)确实存在吗?我知道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 但不知道" Clicked"。无论如何:

{{1}}