total newb to VB, textboxes and loops

  • 13 years ago

      thanks for bearing with a total newb - I've done some programmning before, but it was in college, in C++, and console only :). I'm teaching myself some VB.net, and am having trouble porting some of the concepts forward. I'm writing a practice program to help track my golf scores over time, and report on various statistics. It seems to encompass many of the techniques I might need at work (which is the ultimate goal for learning to code in VB). What I've got are 26 text boxes that I need to grab input from - 18 for the par of the hole, and 18 for my score on that hole. I'm struggling with how I can address each of the textboxes in an iterative manner - for humor, here's the current "get data" procedure ...


        Private Sub bAddScore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bAddScore.Click
            Par(1) = tbH1P.Text
            Par(2) = tbH2P.Text
            Par(3) = tbH3P.Text
            Par(4) = tbH4P.Text
            Par(5) = tbH5P.Text
            Par(6) = tbH6P.Text
            Par(7) = tbH7P.Text
            Par(8) = tbH8P.Text
            Par(9) = tbH9P.Text
            Par(10) = tbH10P.Text
            Par(11) = tbH11P.Text
            Par(12) = tbH12P.Text
            Par(13) = tbH13P.Text
            Par(14) = tbH14P.Text
            Par(15) = tbH15P.Text
            Par(16) = tbH16P.Text
            Par(17) = tbH17P.Text
            Par(18) = tbH18P.Text

            Score(1) = tbH1S.Text
            Score(2) = tbH2S.Text
            Score(3) = tbH3S.Text
            Score(4) = tbH4S.Text
            Score(5) = tbH5S.Text
            Score(6) = tbH6S.Text
            Score(7) = tbH7S.Text
            Score(8) = tbH8S.Text
            Score(9) = tbH9S.Text
            Score(10) = tbH10S.Text
            Score(11) = tbH11S.Text
            Score(12) = tbH12S.Text
            Score(13) = tbH13S.Text
            Score(14) = tbH14S.Text
            Score(15) = tbH15S.Text
            Score(16) = tbH16S.Text
            Score(17) = tbH17S.Text
            Score(18) = tbH18S.Text
        End Sub

    (no error checking or anything yet, I'm just getting started). I'm familiar with the looping syntaxes and usage, but getting the object names (tbHxS or tbHxP) to resolve properly in an iterative fashion has stumped me. (anything prefaced by "tb" is a textbox on the form, and score() and par() are int arrays, 18 elements each).

    If anyone could suggest a manner of constructing a FOR loop to do this, or perhaps a better way to attack the problem as a whole (lots of inputs needed at one time), I would be MUCH appreciative. Obviously, there are lots of things that have to happen each time I grab an input (check for errors, at a minimum), and I'd rather not have to do each one 18 times :). Thank you!

    -Josh

  • 13 years ago

    For a start, .NET arrays are zero-based.  In VB.NET you create an array by specifying the upper bound.  If you want an array with 18 elements then the upper bound is 17.  Those elements then occupy indexes 0 to 17, not 1 to 18.


    You could do this:but I personally would not.

    For i As Integer = 0 To 17 Step 1
    Me.par(i) = CInt(Me.Controls(String.Format("tbH{0}P", i + 1)).Text)
    Me.score(i) = CInt(Me.Controls(String.Format("tbH{0}S", i + 1)).Text)
    Next i
    If you want to loop through controls then I strongly suggest that you create an array:
    Private parFields As TextBox()
    Private scoreFields As TextBox()

    Private Sub Form1_Load(ByVal sender As Object, _
    ByVal e As EventArgs) Handles MyBase.Load
    Me.parFields = New TextBox() {tbH1P, _
    tbH2P, _
    tbH3P, _
    tbH4P, _
    tbH5P, _
    tbH6P, _
    tbH7P, _
    tbH8P, _
    tbH9P, _
    tbH10P, _
    tbH11P, _
    tbH12P, _
    tbH13P, _
    tbH14P, _
    tbH15P, _
    tbH16P, _
    tbH17P, _
    tbH18P}
    Me.scoreFields = New TextBox() {tbH1S, _
    tbH2S, _
    tbH3S, _
    tbH4S, _
    tbH5S, _
    tbH6S, _
    tbH7S, _
    tbH8S, _
    tbH9S, _
    tbH10S, _
    tbH11S, _
    tbH12S, _
    tbH13S, _
    tbH14S, _
    tbH15S, _
    tbH16S, _
    tbH17S, _
    tbH18S}
    End Sub
    So it's a little bit of code. We write code for a living.
  • 13 years ago

     I really am slipping - I didn't even think to build an array of text box references. That worked  beautifully, and really makes the code readable and portable. Thank you for the assistance! Could I ask a follow-up syntactical question? What is the underscore in the code which initalizes the second column of the parFields and scoreFields arrays? Or rather, what does it do? Secondly, why declare the variable private? I ask because this technique will be valuable in future endeavors within this same program, and I'd like to not have to bother you all again for setup help :).

     

    thanks again!!! 

  • 13 years ago

    The underscore is the VB.Net line continuation character and helps to make the code more readable.

    It's unlikely that you'll want to refer to the textbox arrays outside the scope of the form so declaring them private is probably best. 

    Tim

  • 13 years ago

     Thanks, tim - I was making that far mor complicated than it really was. that's also the reason why a Watch only showed me one array entry per element :).

     As for private, now that I read a little farther into this book, that becomes pretty obvious (working with functions and modules now) - guess I shoudl read more and ask less.

     

    Thanks again.
     

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Most software today is very much like an Egyptian pyramid with millions of bricks piled on top of each other, with no structural integrity, but just done by brute force and thousands of slaves” - Alan Kay