简体   繁体   English

将 TextBox 的 NAME 输入到 TextBox 中,然后就可以检索命名的 TextBox 的内容

[英]type the NAME of a TextBox into a TextBox and then be able to retrieve the contents of the named TextBox

for example, I want to declare the textbox based on what I write in the box.例如,我想根据我在框中写的内容声明文本框。 if I write textbox1.如果我写 textbox1. if I type textbox2.text then it will be textbox2.text如果我输入 textbox2.text 那么它将是 textbox2.text

Dim txt As TextBox = TextBox2.Text.ToString
Dim txt As String = TextBox2.Text

none of the methods is valid没有一个方法是有效的

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.您的措辞虽然令人困惑,但似乎您想要将 TextBox 的 NAME 键入到 TextBox 中,然后能够检索命名的 TextBox 的内容。 If that's correct, you're probably looking for Controls.Find() with the recurse option:如果这是正确的,您可能正在寻找带有 recurse 选项的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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM