how can i make textbox(n).text, how can i control n to write something in there

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SizOff
    New Member
    • Oct 2014
    • 2

    how can i make textbox(n).text, how can i control n to write something in there

    Guys! Please help me! For example, i have a programme. There are 4 textboxes and 1 button. So i write in the first textbox a number 2, 3 or 4 and then in one of the textbox programme writes something (doesn't matter what):

    Code:
    Dim x As Integer
    
    Sub Button1.Click(....)
    x = Val(TextBox1.Text)
    TextBox(x).Text = "blahblahblah"
    End Sub
    I hope i wrote it clear and you understand what i want - i want to control with this "x" in which textbox will i write something, but obviously my code isn't working. Please help!
  • iam_clint
    Recognized Expert Top Contributor
    • Jul 2006
    • 1207

    #2
    Assuming you named the controls and know what they are you can simply use the name of the textbox control to modify the value.

    If I assume you don't have the names of the textboxes because they are dynamic you can iterate the controls.
    Code:
     For Each ctrl as control In form1.Controls
       If (ctrl.GetType() Is GetType(TextBox)) Then
          ctrl.text="blahblahblah"
       end if
     Next
    But thirdly if the controls are dynamic you can store references to them in an array when you create them and then modify them that way.

    Comment

    • SizOff
      New Member
      • Oct 2014
      • 2

      #3
      Sorry, i didn't get your message fully (because i don't know english perfectly, yeah), but as I understood you're trying to say that i need to use "If". I tried to do it, and, well, it works, but i use to many code on that:

      Code:
      x = Val(TextBox1.Text)
      if x = 2 Then TextBox2.Text = "blahblahblah"
      if x = 3 Then TextBox3.Text = "blahblahblah"
      if x = 4 Then TextBox4.Text = "blahblahblah"
      So I don't like this version and want just to make like "TextBox(x).Tex t" without "If" or "For", but I don't know how to do it.

      Or maybe I just understood you wrong.

      Comment

      • iam_clint
        Recognized Expert Top Contributor
        • Jul 2006
        • 1207

        #4
        the for loop I posted is probably going to be you're easiest solution. either that or create an array containing references to the textboxes you want.

        I mean if you really wanted TextBox(i).text
        Code:
            Dim TextBox As New List(Of TextBox)
            Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
                For Each ctrl As Control In Me.Controls
                    If (ctrl.GetType() Is GetType(TextBox)) Then
                        TextBox.Add(ctrl)
                    End If
                Next
                TextBox(1).Text = "test"
            End Sub

        Comment

        Working...