将 TextBox 的名称键入 TextBox,然后能够检索命名 TextBox 的内容
type the NAME of a TextBox into a TextBox and then be able to retrieve the contents of the named TextBox
例如,我想根据我在框中写的内容来声明文本框。如果我写 textbox1。如果我输入 textbox2.text 那么它将是 textbox2.text
Dim txt As TextBox = TextBox2.Text.ToString
Dim txt As String = TextBox2.Text
none 个方法有效
你的措辞虽然令人困惑,但看起来你想将 TextBox 的名称键入 TextBox,然后能够检索指定 TextBox 的内容。如果这是正确的,您可能正在寻找具有递归选项的 Controls.Find():
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim txtBoxName As String = TextBox1.Text.Trim
If txtBoxName.Length > 0 Then
Dim ctl As Control = Me.Controls.Find(txtBoxName, True).FirstOrDefault
If Not IsNothing(ctl) AndAlso TypeOf (ctl) Is TextBox Then
Dim tb As TextBox = DirectCast(ctl, TextBox)
' ... do something with "tb" ...
Dim value As String = tb.Text
MessageBox.Show(value)
Else
MessageBox.Show("Control not found, or incorrect type!")
End If
End If
End Sub
例如,我想根据我在框中写的内容来声明文本框。如果我写 textbox1。如果我输入 textbox2.text 那么它将是 textbox2.text
Dim txt As TextBox = TextBox2.Text.ToString
Dim txt As String = TextBox2.Text
none 个方法有效
你的措辞虽然令人困惑,但看起来你想将 TextBox 的名称键入 TextBox,然后能够检索指定 TextBox 的内容。如果这是正确的,您可能正在寻找具有递归选项的 Controls.Find():
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim txtBoxName As String = TextBox1.Text.Trim
If txtBoxName.Length > 0 Then
Dim ctl As Control = Me.Controls.Find(txtBoxName, True).FirstOrDefault
If Not IsNothing(ctl) AndAlso TypeOf (ctl) Is TextBox Then
Dim tb As TextBox = DirectCast(ctl, TextBox)
' ... do something with "tb" ...
Dim value As String = tb.Text
MessageBox.Show(value)
Else
MessageBox.Show("Control not found, or incorrect type!")
End If
End If
End Sub