for example, I want to declare the textbox based on what I write in the box. if I write textbox1. if I type textbox2.text then it will be textbox2.text
Dim txt As TextBox = TextBox2.Text.ToString
Dim txt As String = TextBox2.Text
none of the methods is valid
CodePudding user response:
Your verbiage, although confusing, makes it seem like you want to type the NAME of a TextBox into a TextBox and then be able to retrieve the contents of the named TextBox. If that's correct, you're probably looking for Controls.Find() with the recurse option:
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