0

So if I have lets say 10 textboxes I need to fill I have to repeat a loop 10 times and each time add to a different text box. Right now I have something like this:

    If i = 0 Then
        Shift0 = endTime - startTime
        textStart0.text = startTime
        textEnd0.text = endTime
        chkBox0.checked = True
    End If

I have I repeating like that 8 more times to make 9. I want to make it so that the loop would increase the number from 0-9 every time it goes through

    If i = (x) Then
        Shift(x) = endTime - startTime
        textStart(x).text = startTime
        textEnd(x).text = endTime
        chkBox(x).checked = True
    End If
    x = x + 1

How can I put it in the loop so that the number in the name of the object increased with every loop?

3
  • I think you want arrays - see e.g. msdn.microsoft.com/en-us/library/wak0wfyt.aspx. You should try to keep data out of your variable names. Commented Aug 11, 2014 at 11:02
  • The arrays could have the data in them yes but i want to display that data in a set of textboxes. However instead of writing out each text box I want the loop to do it for me and adding a number to the textbox name like TextBox(x=1), TextBox(x=2).... etc Commented Aug 11, 2014 at 11:05
  • 2
    You can also have a array of textboxes... Commented Aug 11, 2014 at 11:06

3 Answers 3

2

Control arrays are a thing of the past, from the VB6 days, unfortunately, as you've discovered, they can still have their uses! Try this for your loop;

For i = 0 to 9
    Shift0 = endTime - startTime                     ' Is Shift0 a control!?
    FindControl("textStart" & i).Text = startTime
    FindControl("textEnd" & i).Text = endTime
    FindControl("chkBox" & i).Checked = True
Next

With this function to help...

Private Function FindControl(pName As String) As Control
    Dim vMatches = Me.Controls.Find(pName, True)
    If vMatches IsNot Nothing AndAlso vMatches.Length > 0 Then Return vMatches(0)
    Throw New Exception("Could not find the specified control!")
End Function

Having said all that, I would strongly recommend re-thinking how your form and application work to avoid this!

Sign up to request clarification or add additional context in comments.

Comments

1

Something like this would work

For x = 0 to 9
    Shift(x) = endTime - startTime
    textStart(x).text = startTime
    textEnd(x).text = endTime
    chkBox(x).checked = True
next x

Comments

0

You can use the Controls property of a Control with an index. If your form contains exactly and only 10 textboxes, this will work fine:

For i as Integer = 1 to 10
    Form1.Controls(i).Text = "Box " + i.ToString()
Next

If you have other controls in the form you have no guarantee over the index (you can't reply on 1 to 10 being the textboxes as your design progresses). Therefore, I'd recommend you put them inside a panel and refer to this panel's Controls:

For i as Integer = 1 to 10
    Panel1.Controls(i).Text = "Box " + i.ToString()
Next

To learn more about loops in VB.NET, start here: http://www.tutorialspoint.com/vb.net/vb.net_loops.htm

5 Comments

That is just such bad practice... I'm speechless!!
It's certainly more efficient than FindControl, and it's not reliant upon a control's name. There isn't a "good practice" answer to this "bad practice" problem.
That's fair enough - I agree it's not efficient. But then the OPs problem of putting the same data into 10 sets of controls isn't exactly efficient in the first place. And I also agree that the problem is bad practice. Still, iterating controls in a random order, not casting to the correct control types and starting indexes at 1 instead of zero seem worse to me :P
I don't have enough rep to comment on your answer, but if you passed a "searchable" control argument to FindControl (a panel), you wouldn't have to search the whole form. (In both answers, logical sorting is the problem still... I can't think of a better way than sorting them by vertical position!)
I wouldn't waste any more time worrying about it - it's going to be another one of those questions that the OP never closes and will just sit like this for years...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.