Arrays VB.NET窗体数组

Arrays VB.NET窗体数组,arrays,vb.net,forms,Arrays,Vb.net,Forms,假设我有三种形式:Form0、Form1和Form2。 每个表单都包含两个文本框:Tb0和Tb1 在任何形式中都没有其他东西。表单仅在每个文本框中的实际文本(当然还有各个表单的“名称”)上有所不同 表单包含在名为Forms的数组中,即 我想访问(用于读写)Form1中的第一个文本框(Tb0)。我希望能够通过 StringVariable = FORMS(1).Tb0.text or FORMS(1).Tb0.text = "some string" 但上述方法的任何变化都只会产生错

假设我有三种形式:Form0、Form1和Form2。 每个表单都包含两个文本框:Tb0和Tb1

在任何形式中都没有其他东西。表单仅在每个文本框中的实际文本(当然还有各个表单的“名称”)上有所不同

表单包含在名为Forms的数组中,即

我想访问(用于读写)Form1中的第一个文本框(Tb0)。我希望能够通过

 StringVariable = FORMS(1).Tb0.text   or    FORMS(1).Tb0.text = "some string"

但上述方法的任何变化都只会产生错误。两天的“谷歌搜索”毫无结果。建议???

您必须确保
文本框
项在属性中是公共可见的,以便您可以从表单外部访问
文本框

默认情况下,Windows窗体设计器会将
专用
Visual Basic中的朋友
)修改器指定给容器控件,如
面板

现在,您可以访问(读写)
文本框
,如下所示:

Dim FORMS() As Form = {Form0, Form1, Form2}

'read the value
Dim StringVariable As String = FORMS(1).Tb0.Text

'write the value
FORMS(1).Tb0.Text = "some string"

另一个(更灵活的)解决方案:

使用以下解决方案,您还可以在
修改器设置为
Private
的情况下获取并设置
文本框
项的值。如果
表单
上没有
文本框
项,该解决方案也更灵活

'set all the forms to an array.
Dim forms() As Form = {Form1, Form2, Form3}

'search for a specific control on the first form of the array.
Dim foundControls() As Control = forms(0).Controls.Find("TextBox1", True)

'check if the control is available and a TextBox.
If foundControls.Length = 1 AndAlso TypeOf foundControls(0) Is TextBox Then
    Dim txtControl As TextBox = DirectCast(foundControls(0), TextBox)

    'set a value to the TextBox.
    txtControl.Text = "Hello"

    'get the value from the TextBox.
    Debug.Print(txtControl.Text)
End If

'... or using the functions (much easier to use).
SetTextBoxValue(forms(0), "TextBox1", "Hello World")
Dim strValue As String = GetTextBoxValue(forms(0), "TextBox1")
您可以使用函数更轻松地设置和获取
文本框
项的值:

Private Sub SetTextBoxValue(ByVal frm As Form, ByVal txtName As String, ByVal txtValue As String)

    'search for a specific control on the first form of the array.
    Dim foundControls() As Control = frm.Controls.Find(txtName, True)

    'check if the control is available and a TextBox.
    If foundControls.Length = 1 AndAlso TypeOf foundControls(0) Is TextBox Then
        Dim txtControl As TextBox = DirectCast(foundControls(0), TextBox)
        txtControl.Text = txtValue
    End If
End Sub

Private Function GetTextBoxValue(ByVal frm As Form, ByVal txtName As String)

    'search for a specific control on the first form of the array.
    Dim foundControls() As Control = frm.Controls.Find(txtName, True)

    'check if the control is available and a TextBox.
    If foundControls.Length = 1 AndAlso TypeOf foundControls(0) Is TextBox Then
        Dim txtControl As TextBox = DirectCast(foundControls(0), TextBox)
        Return txtControl.Text
    End If

    Return ""
End Function

产生除错误以外的任何东西
如果您告诉我们错误总是很有用的。是
Form0
Form1
还是
Form2
类或实例?如果它们是类,那么您使用的是默认实例,这是代码中两个错误之一,第二个错误是您应该使用控件Tb0或用户控件创建一个基窗体。但是您应该在某个地方使用OOP,因此不需要搜索控件。这个问题和公认的答案不应该被视为做这类事情的正确方式。也许这些实际上只是
Form
并且是动态创建的
控件。Find
很好,但我对此表示怀疑。
Dim FORMS()作为Form={Form0,Form1,Form2}
将项目限制为Form,而Form没有Tb0
Dim FORMS={Form0,Form1,Form2}
应该可以工作。在我的多次迭代中,我想(!!!!!)我尝试过了,但没有成功。无论如何,布罗希先生提出的解决方案解决了我的问题。耶@djv我无法让
Dim FORMS={Form0,Form1,Form2}
工作(假设我在项目中有这些表单)。Option Strict不希望推断对象。'Dim FORMS()作为Form={Form0,Form1,Form2}按预期工作。使用此答案,如果
Form1
不包含对象
Tb0
,则在设计时不会出现任何错误,但在运行时会弹出错误。
Private Sub SetTextBoxValue(ByVal frm As Form, ByVal txtName As String, ByVal txtValue As String)

    'search for a specific control on the first form of the array.
    Dim foundControls() As Control = frm.Controls.Find(txtName, True)

    'check if the control is available and a TextBox.
    If foundControls.Length = 1 AndAlso TypeOf foundControls(0) Is TextBox Then
        Dim txtControl As TextBox = DirectCast(foundControls(0), TextBox)
        txtControl.Text = txtValue
    End If
End Sub

Private Function GetTextBoxValue(ByVal frm As Form, ByVal txtName As String)

    'search for a specific control on the first form of the array.
    Dim foundControls() As Control = frm.Controls.Find(txtName, True)

    'check if the control is available and a TextBox.
    If foundControls.Length = 1 AndAlso TypeOf foundControls(0) Is TextBox Then
        Dim txtControl As TextBox = DirectCast(foundControls(0), TextBox)
        Return txtControl.Text
    End If

    Return ""
End Function