Answered streamline basic code

  • Thursday, September 20, 2012 6:25 AM
     
      Has Code

    hi, i have a program, that has some code something like this....

    If varb = 1 Then TextBox1.Text = vara
            If varb = 2 Then TextBox2.Text = vara
            If varb = 3 Then TextBox3.Text = vara
            If varb = 4 Then TextBox4.Text = vara

    my question is, if i wanted to do this 180 times, is there a way to put this in code with a variable that counts from 1 to 180 and just use 1 line of code?  thanks.... 

All Replies

  • Thursday, September 20, 2012 8:00 AM
     
     Answered

    Try this

           Dim varb As Integer = 1
            Dim mycontrol() As Control = Me.Controls.Find("Textbox" & varb, True)
            mycontrol(0).Text = "abc"

    jdweng

  • Thursday, September 20, 2012 10:03 AM
     
     Answered Has Code

    Hi,

    For i As Integer = 1 To 180
    If varb = i Then 
        Controls.Find("TextBox" & Cstr(i),True)(0).Text = vara
    End If
    Next

    Best wishes
  • Thursday, September 20, 2012 10:48 AM
     
     Answered

    Since it is likely that number of textboxes would be created in code, not in the designer, then you should create an array or list of the textboxes as you create them.   Then you can simply update the textbox using the variable as the index:

        Textboxes(varb).Text = vara


  • Thursday, September 20, 2012 4:41 PM
     
     
    thanks guys, very helpful....
  • Thursday, September 20, 2012 6:39 PM
     
     
    thanks guys, very helpful....

    Please mark as answers the posts that solve your issue, beside to vote as helpful for helpful posts.