[RESOLVED] To use arrays in VB.NET
CIAO
It was possible and easy to declare arrays in VB6
For example if I am using a textbox "Text1"
I copy paste it and an array is "Text1" textbox is declared
having
Text1(0)
Text1(1)
etc
I cannot figure out how to use this in VB.NET
Thanks in advance
Re: To use arrays in VB.NET
AH... what you are asking about are CONTROL ARRAYS (as opposed to simple arrays).
While control arays are no longer supported in VB.NET... depending on your reason for them, there are ways around it.
What did you want to be able to get out of using a control array?
-tg
Re: To use arrays in VB.NET
There are plenty of examples if you search the VB.NET forum for "control array", as this question has came up plenty of times before. The results should have examples of what techgnome has stated.
Re: To use arrays in VB.NET
I need insert records in Text box from another file
I want to use them in a loop
As the loop goes on
for example
Text(a).Text=scr.Readline()
a++
Where scr.Readline() is a method of file system and that may not be very concern.
As the loop goes on the value of variable 'a' increments and then the textbox changes from 1 to 2 and so on.
If the control array is not possible then I have to explicitly state for every insertion of text with every line and why should I do that
Re: To use arrays in VB.NET
I think that the easiest way is just to add all your TextBoxes to the form in the designer then add a member variable in the code window for the array:
VB Code:
Private textBoxes() As TextBox = {Me.TextBox1, Me.TextBox2, Me.TextBox3}
Then you can simply refer to them by their index in that array. Note that arrays zre zero-based in .NET so TextBox1 is at index 0, etc.
Re: To use arrays in VB.NET
dim textBoxes() as TextBox
Is textBoxes a keyword or can I use any other word
I have rename my TextBox1 as tx1
Re: To use arrays in VB.NET
'textBoxes' is just the identifier I used for the array. You can use whatever name you like. Same for the names of the TextBoxes themselves. I just used what would be the default names assigned by the IDE.
Re: To use arrays in VB.NET
Jim
Can you give me the whole code?
I cant figure out how to produce it
Also it is generating errors :
Dim tx1() As TextBox=(Me.TextBox1,Me.TextBox2,Me.TextBox3)
Me.TextBox1, On the comma it is saying: ) expected
Re: To use arrays in VB.NET
You have used parentheses (round brackets) instead of braces {curly brackets} around the three TextBoxes.
Re: To use arrays in VB.NET
ok
jim
But on the design should I put a textbox
Since I put only one textbox on form named tx1 then used your statement
Dim tx1() As TextBox={Me.TextBox1,Me.TextBox2,Me.TextBox3}
but then the error is generated that tx1 is already declared
Should I use textbox with other name or should I do not put a form on design The code will do all the work?
Re: To use arrays in VB.NET
me.textbox1, me.textbox2, me.textbox3 are referring to three different textbox controls on the form....
Re: To use arrays in VB.NET
Not like that. In my code 'textBoxes' is the name of the array and 'TextBox1', 'TextBox2' and 'TextBox3' are the names of the TextBoxes that you've added to the form in the designer. If you've added one TextBox named tx1 in the designer then you would have to change my code to look like this:
VB Code:
Private textBoxes() As TextBox = {Me.tx1}
Get it? Each TextBox has to have a different name, and those names are what goes inside the braces because that's what you're adding to the array. The array itself must have a different name again. Forget VB6 and ControlArrays. They are a thing of the past. This is VB.NET and this is an array of TextBoxes following the standard rules for .NET arrays.
Re: To use arrays in VB.NET
ok that resolved
now to use them in loop
suppose 'i' a variable
so My code will look like this?
Private txtBx() As TextBox = {Me.TextBox1, Me.TextBox2, Me.TextBox3}
loop
Me.TextBox(i)
loop
Re: To use arrays in VB.NET
No. Like I said, forget VB6 and ControlArrays. This is a standard .NET array, which is basically the same as a standard VB6 array as far as I'm aware, other than it's zero-based. The name of the array in your example is 'txtBx', so you need to iterate over the elements of 'txtBx'.
VB Code:
For i As Integer = 0 To Me.txtBx.GetUpperBound(0) Step 1
MessageBox.Show(Me.txtBx(i).Name)
Next i
This code will display MessageBoxes containing the names of the TextBoxes, i.e. TextBox1, TextBox2 and TextBox3.
Once you've defined that array you can use txtBx(0) and TextBox1 interchangeably, as they are simply two different references to the same object. It's like if you had two children you could refer to 'my first child' and 'my second child' or you could call them by name, but you'd still be referring to the same person, just in two different ways.
Re: To use arrays in VB.NET
As I said, an array is an array in .NET. If you don't understand arrays then here's something you should read: http://www.homeandlearn.co.uk/NET/nets6p1.html
Re: To use arrays in VB.NET
Quote:
Originally Posted by jmcilhinney
Its not arrays its control arrays
I ahve played alot on arrays and your link does not provide info abt control arrays and if u say that arrays and control arrays are same
I have put three textboxes on my form deign
and the code is throwing me Nullexception :-
Public Class Form1
Inherits System.Windows.Forms.Form
Private textBoxes() As TextBox = {Me.TextBox1, Me.TextBox2, Me.TextBox3}
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
textBoxes(0).Text = "1"
textBoxes(1).Text = "2"
textBoxes(2).Text = "3"
End Sub
End Class
Please give me info abt control arrays
Re: To use arrays in VB.NET
I will say it again: forget VB6 and ControlArrays. ControlArrays DO NOT EXIST in VB.NET. There is no design time support for control arrays in VB.NET and controls have no Index property. That's what made a ControlArray in VB6 and it does not exist in VB.NET. All you can do is make a standard array of controls, as we're trying to do here, and access the elements by index.
As for the exception, that's my mistake. You can't initialise the array like that because each of the TextBox variables is Nothing at that point. You would need to populate the array in the constructor at the earliest, but later would be OK too. This is from VB 2005 Express:
VB Code:
Private textBoxes() As TextBox
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
textBoxes = New TextBox() {Me.TextBox1, Me.TextBox2, Me.TextBox3}
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.textBoxes(0).Text = "first"
Me.textBoxes(1).Text = "second"
Me.textBoxes(2).Text = "third"
End Sub
Re: To use arrays in VB.NET