以两种不同的形式访问运行时创建的控件

时间:2017-08-10 12:45:12

标签: vb.net winforms visual-studio-2013

所以我有两种形式,(Form1和Form2)。 Form2询问需要在Form1中生成多少个特定面板(TxtBoxPanel)行。该面板将包含三个框,因此如果用户说要在Form1中生成5行,那么将有5个面板,每个面板有3个文本框。

Form2如图所示: Form2

以下是Form2中的代码:

Public Class Form2
Public Rows As Integer

Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Ok.Click


    Rows = RowNum.Text  'Row num being box to enter no. of rows



    For index = 1 To Rows

        Dim TxtBoxPanel(index)   'Control Array

        Dim LeftBox(index) 'Control Array
        Dim CenterBox(index) 'Control Array
        Dim RightBox(index) 'Control Array
        Dim YAxis As Integer ' for adding TxtBoxPanel in new row

        'adding the textbox panel

        TxtBoxPanel(index) = New Panel

        Form1.MajorPanel.Controls.Add(TxtBoxPanel(index))  'referring to form1 as panel needed in form1
        TxtBoxPanel(index).Name = ("txtBoxPanel" & index)
        TxtBoxPanel(index).Size = New Size(610, 32)
        YAxis += 32
        TxtBoxPanel(index).Location = New Point(3, YAxis)

        'adding left box
        LeftBox(index) = New TextBox

        TxtBoxPanel(index).Controls.Add(LeftBox(index))
        LeftBox(index).Name = ("LeftBox" & index)



        LeftBox(index).Text = (index)
        LeftBox(index).Size = New Size(100, 20)
        LeftBox(index).Location = New Point(3, 3)

        'adding center box
        CenterBox(index) = New TextBox

        TxtBoxPanel(index).Controls.Add(CenterBox(index))
        CenterBox(index).Name = ("CenterBox" & index)
        CenterBox(index).Text = (index)
        CenterBox(index).Size = New Size(100, 20)
        CenterBox(index).Location = New Point(258, 3)

        'adding right box
        RightBox(index) = New TextBox

        TxtBoxPanel(index).Controls.Add(RightBox(index))
        RightBox(index).Name = ("RightBox" & index)
        RightBox(index).Size = New Size(100, 20)
        RightBox(index).Text = (index)
        RightBox(index).Location = New Point(495, 3)



    Next index

    Close()  'After generation of controls, Form2 closes

End Sub



End Sub
End Class

这些框在Form1中生成良好,如下图所示: Controls in Form1

现在,我想遍历" LeftBox"的每个实例。这样我就可以打印出#34; Name"当我点击"消息"每个实例的属性Form1上的按钮。但是,生成LeftBox的代码位于Form2中,我发现很难在Form1中引用它。

请注意我使用数组生成控件,因为我在某处读取了与使用Controls.Find相比更容易引用控件

所以,问题是,如何在运行时生成Form1中的控件?

这就是我参考LeftBox的方法,例如,早些时候:

  ' FINDING CONTROLS PROGRAMATICALLY


    For Index = 1 To Form2.Rows

        Dim LBox As TextBox

        For Each LBox In Me.Controls.Find("LeftBox" & Index, True)
            If (LBox.Name.Contains("LeftBox") = True) Then

                MsgBox(LBox.Text, MsgBoxStyle.Information, "Testing")

            Else
                MsgBox("There's a problem", MsgBoxStyle.Information, "Testing")


            End If

        Next

    Next

谢谢。

1 个答案:

答案 0 :(得分:0)

所以最后在尝试了几个小时后,我使用List来存储文本框的每个实例,这使我可以在以后引用它们。这是以下代码:

 Public Class Form2 
Public Rows As Integer

Public Sub Button1_Click(sender As Object, e As EventArgs) Handles OK.Click


    Rows = RowNum.Text
    Vars.Indx = Rows 'for storing the value of rows globally 


    For index = 1 To Rows

        Dim TxtBoxPanel
        Dim LeftBox
        Dim CenterBox
        Dim RightBox
        Dim YAxis As Integer  ' for adding TxtBoxPanel in new row

        'adding the textbox panel

        TxtBoxPanel = New Panel

        Form1.Controls.Add(TxtBoxPanel)
        TxtBoxPanel.Name = ("txtBoxPanel" & index)
        TxtBoxPanel.Size = New Size(610, 32)
        YAxis += 30
        TxtBoxPanel.Location = New Point(24, YAxis)

        'adding left box
        LeftBox = New TextBox

        TxtBoxPanel.Controls.Add(LeftBox)
        LeftBox.Name = ("LeftBox" & index)



        LeftBox.Text = (index)
        LeftBox.Size = New Size(100, 20)
        LeftBox.Location = New Point(3, 3)

        LeftList.Add(LeftBox)  'inserts LeftBox to list 
        'adding center box
        CenterBox = New TextBox

        TxtBoxPanel.Controls.Add(CenterBox)
        CenterBox.Name = ("CenterBox" & index)
        CenterBox.Text = index
        CenterBox.Size = New Size(100, 20)
        CenterBox.Location = New Point(258, 3)
        CenterList.Add(CenterBox)  'inserts centerbox to list
        'adding right box
        RightBox = New TextBox

        TxtBoxPanel.Controls.Add(RightBox)
        RightBox.Name = ("RightBox" & index)
        RightBox.Size = New Size(100, 20)
        RightBox.Text = index
        RightBox.Location = New Point(495, 3)

        RightList.Add(RightBox) 'adds rightbox to list

    Next index



    Close()  'After generation of controls, Form2 closes

End Sub

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles RowNum.TextChanged

End Sub
 End Class

为了引用Form1中的文本框,我在一个名为Vars的模块中声明了List,如下所示:

Module Vars
'for storing variables needed globally
'whatever needs to be "connected" to different forms, store it here

Public Indx As Integer
Public CenterList As New List(Of TextBox)   
Public RightList As New List(Of TextBox)
Public LeftList As New List(Of TextBox)

 End Module

然后在Form1中,这就是我所做的:

Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Form2.Show()
End Sub



Private Sub Message_Click(sender As Object, e As EventArgs) Handles Message.Click


    For index = 0 To (Vars.Indx - 1) 'loop through leftbox 

        MsgBox(LeftList(index).Text, MsgBoxStyle.Information, "INFO")


    Next



End Sub
End Class

它的工作,但我仍然想知道这段代码是否真的有效?