HELP putting an integer in the middle of a string
So I have a bunch of text boxes on a windows form named, "XB1TextBox", "XB2TextBox", "XB3TextBox", etc. all the way up to 30.
I want to populate them with values from an array, called T, that is 30 members long. So in the form load even I have something like this
Dim Count As Integer = 1
For Count = 1 to 30
XB & Count & TextBox.Text = T(Count - 1 )
Next
Line 3 of this code is obviously incorrect... Does anyone have any ideads??? Thank you so much for your help!!
Kevin
Re: HELP putting an integer in the middle of a string
so what you're trying to do is dynamically assign the textbox you want to populate?
Re: HELP putting an integer in the middle of a string
Re: HELP putting an integer in the middle of a string
are those textboxes the only textboxes on the form? If so I'm pretty sure you can loop through all the controls you have on the form.
Now don't quote me on this but you could probably write a nested loop. The outer loop being the "count" loop with the inner being the controls. Then test if the control is blank or not, if it's blank fill it in with the appropriate value and exit the inner loop to move onto the next outer loop iteration. If it isn't blank iterate through the inner loop until you find a control that is blank. This is also assuming everything is in numerical ascending order and that all text boxes are clear before you begin.
Don't know firsthand if it would work or not because i've personally never done something like that. Maybe someone who has can chime in as well.
**edit** also, for the control loop, you should make sure to specify you only want textboxes so you don't try and assign a value to something like a button lol
Re: HELP putting an integer in the middle of a string
http://www.vbforums.com/showthread.p...=Loop+controls
here you go, this was the thread i was thinking of. See if you can make some sense of it and get it to work.
Re: HELP putting an integer in the middle of a string
vb Code:
Dim Count as Integer = 1
Dim currentIndex as Integer = 0
For Count = 1 to 31 'You need to go to 31 because indexes are 0-based
DirectCast(Me.Controls("XB" & Count.ToString & "TextBox"), TextBox).Text = T(currentIndex.ToString)
currentIndex+=1
Next
See if that works. The syntax on the DirectCast may be off a bit, but should be very close. I'm unable to test at the moment
Re: HELP putting an integer in the middle of a string
That worked great. Thanks so much for your help!!!
Re: HELP putting an integer in the middle of a string
Good deal. Rate the posts if you'd like and mark thread resolved.
Glad you got it workin!
Re: HELP putting an integer in the middle of a string
Sorry, first time to this forum. Where do you mark thread resolved?
Re: HELP putting an integer in the middle of a string
Oh yea! Welcome!
Right above the first post, it says "Thread Tools". Click that, and on the dropdown, select "Mark Thread Resolved"
Re: HELP putting an integer in the middle of a string
Quote:
Originally Posted by
stateofidleness
vb Code:
Dim Count as Integer = 1
Dim currentIndex as Integer = 0
For Count = 1 to 31 'You need to go to 31 because indexes are 0-based
DirectCast(Me.Controls("XB" & Count.ToString & "TextBox"), TextBox).Text = T(currentIndex.ToString)
currentIndex+=1
Next
See if that works. The syntax on the DirectCast may be off a bit, but should be very close. I'm unable to test at the moment
Just as a side note here, you don't need to cast a control type to a textbox type just to access its .text property. The text property is inherited from the base control class anyway. You only need to cast when you need to access something specific to the given class, that is not inherited from the base class you already have an instance of.
Also, 1 to 31 is 31 iterations, not 30... 0 to 29 or 1 to 30 is 30 iterations of a loop.
I would code this out something like this:
Code:
For Count As Integer = 1 To 30
Me.Controls(String.Format("XB{0}TextBox", Count.ToString)).Text = T(Count - 1)
Next
where I am assuming T is some 30 element array of strings?
Re: HELP putting an integer in the middle of a string
AH! very cool. Not too good with the String format stuff yet.
Also, good tip on the casting.