close

Privacy guaranteed - Your email is not shared with anyone.

Visual Basic, anyone?

Discussion in 'Tech Talk' started by Super Shorty, Dec 14, 2005.


  1. Super Shorty

    Super Shorty
    Expand Collapse
    5 feet of fun!

    Joined:
    Dec 7, 2005
    17
    0
    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.
     

    Wanna kill these ads? We can help!
  2. grokdesigns

    grokdesigns
    Expand Collapse

    Joined:
    Dec 23, 2004
    507
    0
    Location:
    Chandler, AZ
    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
    TextBoxCollection.Add(Me.Name1TextBox)
    TextBoxCollection.Add(Me.Name2TextBox)
    TextBoxCollection.Add(Me.Name3TextBox)
    For Each objTextBox In TextBoxCollection
    sngSum = sngSum + Val(objTextBox.Text)
    Next objTextBox
     

  3. Super Shorty

    Super Shorty
    Expand Collapse
    5 feet of fun!

    Joined:
    Dec 7, 2005
    17
    0
    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!
     
  4. grokdesigns

    grokdesigns
    Expand Collapse

    Joined:
    Dec 23, 2004
    507
    0
    Location:
    Chandler, AZ
    I think I figured out a solution for you, I have this run on a Button_Click event:

    Code:
    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
    Next
    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.
     
  5. Super Shorty

    Super Shorty
    Expand Collapse
    5 feet of fun!

    Joined:
    Dec 7, 2005
    17
    0
    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)
    Next

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

    Nyper
    Expand Collapse

    Joined:
    Jan 2, 2004
    351
    0
    Location:
    Lebanon TN
    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

    [​IMG]
     
  7. Super Shorty

    Super Shorty
    Expand Collapse
    5 feet of fun!

    Joined:
    Dec 7, 2005
    17
    0
    Wow, I bet THAT was fun... :p