Control Array

R

Robert

New to .Net...
Always used VB 6.0

Question:
In VB, I could have a textbox, and have an array of 10, examples text1(0),
text1(1) etc.

How would I do this in VB.net (VS 2008 Standard Edition)
or do you have to have 10 seperate text boxes?
 
P

Phill W.

Robert said:
In VB, I could have a textbox, and have an array of 10, examples text1(0),
text1(1) etc.

VB 'Proper's "Control Arrays" are Dead and Gone.
Instead, you can now create Arrays of Controls.

Start with the ten individual TextBoxes, then add them all into an
array, usually in Form_Load.

Dim m_tbs as TextBox() = Nothing

Private Sub Form_Load( ... ) _
Handles Mybase.Load

m_tbs = New TextBox() { tb1, tb2, ..., tb10 }

End Sub

Then, at any point, you can loop through that array, doing whatever you
need to do.

For Each tb as TextBox in m_tbs
tb.Text = String.Empty ' say
Next

To handle events on [all] the TextBoxes, use multiple "Handles" clauses
on a single event handler and the sender argument to identify the
calling control, as in

Private sub TB_EnabledChanged( ... ) _
Handles tb1.EnabledChanged _
, tb2.EnabledChanged _
, ... _
, tb10.EnabledChanged

With DirectCast( sender, TextBox )
.BackColor = ...
End With

End Sub

If you're adding the TextBoxes dynamically, use the AddHandler statement
to "wire-up" the event handlers.

HTH,
Phill W.
 
G

Göran Andersson

Robert said:
New to .Net...
Always used VB 6.0

Question:
In VB, I could have a textbox, and have an array of 10, examples text1(0),
text1(1) etc.

How would I do this in VB.net (VS 2008 Standard Edition)
or do you have to have 10 seperate text boxes?

The designer doesn't support control arrays.

You can cretate ten textboxes in the designer and add the reference to
an array.

Or, instead of having the designer create the code that adds the
controls to the form, add the controls yourself and put the references
in an array. (This of course means that they will not be available in
the designer.)
 
R

Robert

Thanks for your replies.
Question then, is there any good samples showing a simple program with the
arrays?
 
M

Mike Williams

So are you now trolling the .NET group to somehow
"get back" at Bill McCarthy?

Yep. Your ex fellow MVP McCarthy is trolling the VB Classic group and that
doesn't seem to worry you too much, so why should I worry about a bit of
trolling on the dotnet group?

Thanks

Michael
 
R

rowe_newsgroups

Yep. Your ex fellow MVP McCarthy is trolling the VB Classic group and that
doesn't seem to worry you too much, so why should I worry about a bit of
trolling on the dotnet group?

Thanks

Michael

I'm sure you're not dumb enough to believe that I have some way of
stopping him? Even if everyone in this group knows about your "war"
with McCarthy, there is nothing we could do.

But if choose to keep up your childish agenda by trolling this
newsgroup then have fun. We can all just killfile or ignore you like
I'm sure McCarthy has been.

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
 
M

Mike Williams

I'm sure you're not dumb enough to believe that I have some
way of stopping him [Bill McCarthy]? Even if everyone in this
group knows about your "war"with McCarthy, there is nothing
we could do. But if choose to keep up your childish agenda by
trolling this newsgroup then have fun. We can all just killfile or
ignore you like I'm sure McCarthy has been.
Thanks,

Thank you for your sage advice, Seth. I assume that as an MVP you would feel
the same about McCarthy's spamming of the Micro$oft Classic VB newsgroup,
especially as he was an MVP himself for much of the time he was doing it, so
I shall ask McCarthy to take it into account next time he considers spamming
the group.

Thanks

Michael
 
E

eBob.com

I'm not an expert at this stuff but I think that this should get you
started. Although I used VS I added nothing to the form using VS, all of
the textboxes are added dynamically.

Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim tbxArray(5) As TextBox
Dim xloc As Integer = 20
Dim yloc As Integer = 20
Dim yinc As Integer = 30
For i As Integer = 0 To 5
tbxArray(i) = New TextBox
tbxArray(i).Location = New Point(xloc, yloc + (i * yinc))
tbxArray(i).Text = "Box " & i.ToString
Controls.Add(tbxArray(i))
Next
End Sub
End Class

Good Luck, Bob


Robert said:
Thanks for your replies.
Question then, is there any good samples showing a simple program with the
arrays?
 
G

Göran Andersson

Robert said:
Thanks for your replies.
Question then, is there any good samples showing a simple program with the
arrays?

If you create the controls in the designer, putting them in an array is
simple:

Dim foo As TextBox() = {foo0, foo1, foo2, foo3}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Newbie 7
Upgrading older VB programs (sans Project Files) to VB.NET 34
simple datagrid help 2
exe running from unc 3
String array problem 5
Creating Control Array 10
What's the point of C# and C++ 53
Control Array 5

Top