所以我有兩種形式(Form1和Form2)。 Form2詢問需要在Form1中生成多少行特定面板(TxtBoxPanel)。該面板將包含三個框,因此如果用戶說要在Form1中生成5行,則會有5個面板,每個面板有3個文本框。以兩種不同形式訪問在運行時進行的控制
窗體2顯示爲所示的圖像中: Form2
下面是在窗體2的代碼:
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」實例,以便在單擊Form1上的「消息」按鈕時可以打印出每個實例的「Name」屬性。但是,生成LeftBox的代碼位於Form2中,我發現很難在Form1中引用它。
請注意,我生成使用數組的控件,因爲我讀的地方,他們更容易地引用對照組相比,使用Controls.Find
所以,問題是,我怎麼能引用控制在Form1中後,他們在運行時生成?
這是我wtried引用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
謝謝。
'Form1'有一個'Controls'集合,它將包含所有的控件。你檢查了嗎?顯示您用來訪問控件的代碼。 –
請不要把代碼放入評論中。相反,請編輯您的問題並在其中添加代碼。我有點困惑。我認爲你所有的'TxtBoxPanel'控件都在'Form1'上。你添加的代碼似乎在'Form2'中尋找它們? –
對不起@Chris,我只是編輯了包含代碼的問題。 TxtBoxPanel確實生成到Form1上,但生成面板到form1上的代碼位於form2上。這就是爲什麼我認爲我必須參考form2,因爲代碼實際上在那裏 – Amin84