Visual Basic, anyone? [Archive] - Glock Talk


View Full Version : Visual Basic, anyone?

Super Shorty
12-14-2005, 14:40
Anybody here program in Visual Basic?
I'm a relative beginner, and have a program that deals with a lot of text boxes for bringing in numbers to do calculations. I'm wondering if there's any efficient way of using a loop of some sort to cycle through the text boxes, rather than bringing in each value one by one and parsing to a numeric value. I thought of just naming the text boxes by 0, 1, 2,..., but that does not follow conventional naming standards.
Maybe there's some way to use tab order for that? (note: all text boxes are in a group box, does that help?)
Any help is appreciated.

12-14-2005, 18:08
Depends on the version...

Visual Basic 6.0 code (Using a control array)
For intX = 0 To 3
sngSum = sngSum + Val(txtSales(intX).Text)
Next intX

Visual Basic.NET code

Dim TextBoxCollection As New Collection()
Dim objTextBox As TextBox
For Each objTextBox In TextBoxCollection
sngSum = sngSum + Val(objTextBox.Text)
Next objTextBox

Super Shorty
12-15-2005, 09:42
It's in Visual Basic.Net.
So if I'm understanding this correctly, you still have to add the text boxes to the collection one by one, not really making the code any shorter, right?
I suppose if there were more than one place to bring in values, that might come in handy, but for my particular case it's just the one place. Thanks for showing me something new, though!

12-15-2005, 16:48
I think I figured out a solution for you, I have this run on a Button_Click event:

Dim x() As Object = {TextBox1, TextBox2, TextBox3}
Dim value As Double
Dim i As Integer
value = "0"
For i = 0 To x.Length - 1
value = x(i).text + value
TextBox4.Text = value

Just replace the TextBox1, TextBox2, etc. with the names of the text boxes you want included. It will then loop through all listed text boxes and return the string to TextBox4.

Super Shorty
12-16-2005, 11:29
grokdesigns -

That was actually a good idea...after looking at it a bit I'm wondering why I didn't think of this before...
I simply declared a new array of type TEXTBOX (I did not know you could do this) and listed the text boxes on my form in the array and then used a loop:

Dim textBox() As TextBox = {textBox1, textBox2,...textBox13)

For index = 0 to 12
numArray(index) = Integer.Parse(textBox(index).Text)

(for example close to what I have).
It works exactly the same now and is much simpler. Thank you!

12-16-2005, 14:10
I feel your pain.
I just finished writing a program for a friend to calculate golf matches. It consisted of a 27 hole scorecard (yes, 27 text boxes), as well as a scorecard to enter the pars for each hole (another 18 text boxes). I have a jillion loops in there to cycle through my textbox arrays. ;Q

Super Shorty
12-18-2005, 18:21
Wow, I bet THAT was fun... :P