【问题标题】:VB.NET array of formsVB.NET 表单数组
【发布时间】:2019-10-03 20:49:05
【问题描述】:

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

在任何形式中都没有其他内容。表单的不同之处仅在于每个文本框中的实际文本(当然还有各个表单的“名称”)。

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

  FORMS = {Form0,Form1,Form2}

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

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

但上述方法的任何变体都不会产生任何错误。两天的“谷歌搜索”都没有结果。建议??

【问题讨论】:

  • produces anything but errors 如果您告诉我们错误总是有帮助的。
  • Form0Form1Form2 是类还是实例?如果它们是类,那么您使用的是默认实例,这是您的代码有两个问题之一,第二个是您应该使用控件 Tb0 或用户控件创建一个基本表单。但是你应该在某个地方使用 OOP,所以你不需要搜索控件。这个问题和公认的答案不应被视为做这类事情的正确方法。也许这些实际上只是 Form 并且是动态创建的,然后 Controls.Find 很好,但我对此表示怀疑。

标签: arrays vb.net forms


【解决方案1】:

您必须确保 TextBox 项目对 Modifiers 属性是公开可见的,以便您可以从表单外部访问 TextBox 项目。

默认情况下,Windows 窗体设计器将private(Visual Basic 中为Friend)修饰符分配给容器控件,如Panel

现在您可以访问(读取和写入)TextBox,如下所示:

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"

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

如果Modifiers 设置为Private,您也可以通过以下解决方案获取和设置TextBox 项目的值。如果TextBox 项目在Form 上不可用,该解决方案也更加灵活。

'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")

您可以使用函数更轻松地设置和获取TextBox 项的值:

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

【讨论】:

  • Dim FORMS() As Form = {Form0, Form1, Form2} 将项目限制为没有 Tb0 的 Form。 Dim FORMS = {Form0, Form1, Form2} 应该可以工作。
  • 在我的多次迭代中,我认为(!!!!!)我尝试过但没有成功。无论如何,布罗施先生提供的解决方案解决了我的问题。耶!
  • @djv 我无法让Dim FORMS = {Form0, Form1, Form2} 工作(假设我在项目中有这些表格)。 Option Strict 不想推断对象。 'Dim FORMS() As Form = {Form0, Form1, Form2}' 按预期工作。
  • 有了这个答案,如果 Form1 不包含对象 Tb0 但在运行时会弹出错误,则在设计时不会出现任何错误。
猜你喜欢
  • 2018-09-21
  • 1970-01-01
  • 2013-01-14
  • 1970-01-01
  • 1970-01-01
  • 2014-08-22
  • 1970-01-01
  • 2020-05-20
  • 1970-01-01
相关资源
最近更新 更多