我创建了一个用于编辑数据库记录的屏幕。用户选择表后,我在TableLayoutPanel1中动态添加了Labels和TextBox。我想知道如何访问TableLayoutPanel1
中TextBox中的值我使用的代码:
Dim strSQL, strSQL1 As String
Dim ln As Integer
Dim reader As OleDbDataReader
Dim connection As OleDbConnection
Dim ds As New DataSet
connection = New OleDbConnection(CONNECT_STRING)
connection.Open()
ln = 0
strSQL = " select * from syscat.columns where TABSCHEMA like 'QA1MM%' and TABNAME like 'SKU_STR_LIST' with ur "
RichTextBox1.Text = RichTextBox1.Text + strSQL
Dim selectCMD As OleDbCommand = New OleDbCommand(strSQL, connection)
reader = selectCMD.ExecuteReader
MessageBox.Show("Column: " & reader(0) & " ")
While reader.Read()
If String.IsNullOrEmpty(reader(0)) Then
Else
TableLayoutPanel1.ColumnCount = 2
TableLayoutPanel1.RowCount = 20
Dim aLabel As New System.Windows.Forms.Label
Dim aTextBox As New System.Windows.Forms.TextBox
aLabel.Name = "New Label"
aLabel.Text = reader(0).Text
TableLayoutPanel1.Controls.Add(aLabel, 0, ln)
TableLayoutPanel1.Controls.Add(aTextBox, 1, ln)
ln = ln + 1
End If
End While
如何访问我在TableLayoutPanel1中动态添加的aTextBox中的值?
答案 0 :(得分:3)
首先,您需要在TextBox上设置名称。基本上aTextbox.Name =“TextBox1”。然后你应该能够使用:
找到控件Control c = TableLayoutPanel1.Controls.Find("TextBox1",true)
但请记住,这会给你一个控件,而不是文本框。所以你可以用任何你认为合适的方式对它进行类型化。
Ctype(c,TextBox)
DirectCast(c,TextBox)
答案 1 :(得分:1)
正如WozzeC所说,您需要为文本框指定一个独特的名称,可能类似于
aTextBox.Name = "TextBox" & ln
然后你可以这样找到它
Dim words = TableLayoutPanel1.Controls("Textbox1").Text
或者它在同一个循环中
Dim words = TableLayoutPanel1.Controls("Textbox" & ln).Text