Added control not declared?

gem_max

Member
Joined
Aug 28, 2006
Messages
5
Programming Experience
Beginner
Hello. I've come from a c++ background and learning VB.NET has been confusing for some reason cos of this.

Anywho, I have a 2d array of textboxes (gridtextbox(i,j)) that are generated in the load procedure and work perfectly. However when I try to code the click event that calculates the sum and average of each row of the grid the gridtextbox array tells me it's undeclared despite the Me.Controls.Add(gridTextBox(i, j)) line that appears in each loop during the textbox array creation.

I just can't get my head around VB and cant find useful sites anywhere plus our text is hopeless so from the information above can anyone tell me what i might need to do? If more information is needed just let me know.

Thanks for the help!
 
OK here the code for creating the textbox array:

VB.NET:
Const rows As Integer = 3
Const columns As Integer = 3
Const xGap As Integer = 58
Const yGap As Integer = 48
 
Dim i As Integer
Dim j As Integer
'declare array of textboxes
Dim gridTextBox(rows, columns) As TextBox
 
'create each element in the array
For i = 0 To rows
For j = 0 To columns
gridTextBox(i, j) = New TextBox
gridTextBox(i, j).Location = New Point(40 + (i * xGap), 135 + (j * yGap))
gridTextBox(i, j).Size = New System.Drawing.Size(30, 45)
gridTextBox(i, j).Text = Val(i) & " " & Val(j)
gridTextBox(i, j).TextAlign = HorizontalAlignment.Right
Me.Controls.Add(gridTextBox(i, j))
Next j
Next i
I have deleted the sub in which I was trying to assign the values of the textboxes to an integer array using a loop but it looked something like:

VB.NET:
for i = 0 to rows
for j = 0 to columns
gridArray(i,j) = Val(gridTextBox(i, j).text)
etc etc
and gridtextbox would produce the undeclared error... though if I've added them to the controls shouldnt i then be able to access them as such? I know this is probably all down to some dumb mistake, but VB just isnt my thing it seems...
 
Last edited by a moderator:
You have declared 'gridtextbox' as a local varaible and so it is only accessible inside that method. If you want it to be visible to the whole class then you need to declare it outside of any methods and use a modifier like the following

VB.NET:
Private gridTextBox(,) As TextBox

Then in your sub re-dimension the array to what size you want it to be...

VB.NET:
Const rows As Integer = 3
Const columns As Integer = 3
Const xGap As Integer = 58
Const yGap As Integer = 48
 
Dim i As Integer
Dim j As Integer
'declare array of textboxes
[COLOR=red]ReDim gridTextBox(rows, columns) [/COLOR]
 
'create each element in the array
For i = 0 To rows
For j = 0 To columns
gridTextBox(i, j) = New TextBox
gridTextBox(i, j).Location = New Point(40 + (i * xGap), 135 + (j * yGap))
gridTextBox(i, j).Size = New System.Drawing.Size(30, 45)
gridTextBox(i, j).Text = Val(i) & " " & Val(j)
gridTextBox(i, j).TextAlign = HorizontalAlignment.Right
Me.Controls.Add(gridTextBox(i, j))
Next j
Next i


You can then reference that array from anywhere inside that class, and depending on the scope of the modifier you have assigned it, anywhere in your application, and beyond..


VB.NET:
Dim Total as Integer
For i as Integer = 0 to GridTextBox.GetUpperBound(0)
For a as Integer = 0 to GridTextBox.GetUpperBound(1)
 
Total += Convert.ToInt32(GridTextBox(i,a).Text)
 
Next 
Next
 
MessageBox.Show(Total.ToString)

So the above code will loop through all the textboxes in the array and add the value of each textbox into the total variable and when it is done it will display it in a messagebox.
 
Thanks! As soon as you exposed me to the local variable thing I went and found the private declaration syntax and whattaya know all the examples throughout the book did not use it so I'd remained oblivious. You guys should put together a book from all the forum posts. Cheers!
 
Back
Top