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 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. Thank you!
-Josh
Loading
AlanPosted Dec 31, 2007, 10:57 AM
Yes, putting your TextBoxes into arrays is a good solution if you're going to need a lot of code like this in your application. As I wasn't sure, I thought I'd show you a different way which eliminates the need to create an array.
If you think of 'Me' in VB.Net as being the equivalent of 'this' in C++, then you won't go far wrong.
Although it's possible to concatenate numbers to strings in VB.Net without converting them to strings first, I always convert them to avoid some strange results that you otherwise occasionally see.
Incidentally, although '+' works, it's better to use the '&' operator to concatenate strings in VB.Net. As I use C# (which uses '+') much more than VB.Net, I often forget this and use '+' in the latter as well :)
JoshPosted Dec 31, 2007, 9:49 AM
I knew that VB arrays were zero-based, but I was using 1-18 for readability :), cause I'm a bit lazy.
AlanPosted Dec 31, 2007, 5:57 AM
If you're using VB 2005 or later, then this code should work:
Private Sub bAddScore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bAddScore.Click
Dim tbName As String
Dim c As Control
For i As Integer = 1 To 18
tbName = "tbH" + i.ToString() + "P"
c = Me.Controls.Item(tbName)
Par(i) = c.Text
tbName = "tbH" + i.ToString() + "S"
c = Me.Controls.Item(tbName)
Score(i) = c.Text
Next i
End Sub
The idea here is to construct the name of the TextBox and then pick it out of the Form's Controls collection, which contains all the controls on the form. Having done that, you can then assign the Control's Text Property to the appropriate member of the array.
Although it doesn't matter much here, one thing I'll point out where VB.Net differs from C++ is that, if you declare an array in the former to have 18 elements, you actually get 19 because there's an element at index 0 as well.