[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
VBNET_CNTRL_Arrays
I've seen many people inquire regarding Control Arrays in VB.net or the apparent lack thereof. Well, that is why I decided to write this article.
In good ol' VB 6, you could put a control on a form, copy it and paste several times, and it would automatically be added to an array. The events of the elements of array could be handled by a single event handler. In VB.net, sadly, it isn't as easy as copy, paste, paste, paste. In this article, I wish to strate how to duplicate - to an extent - the functionality of a control array in VB.net.
First of all, there are two advantages to a control array.
There are two ways to achieve this: There is a control by Eric Porter that allows you to add different controls to a control array or, you can do it in the following manner:
1) Create an array of the control that you wish to use.
Eg.
Eg.
3) Add the events themselves.
Eg.
And we have now acheived dynamically created, control array enabled controls on the form!
One last point regarding handling events: Lets say that you already have a few controls on the form that share a common event type (Eg. On_Click - both an editbox and a button have it...) and you want to make them share an event, all you need to do is just use the AddHandler keyword.
Relman
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
VBNET_CNTRL_Arrays
I've seen many people inquire regarding Control Arrays in VB.net or the apparent lack thereof. Well, that is why I decided to write this article.
In good ol' VB 6, you could put a control on a form, copy it and paste several times, and it would automatically be added to an array. The events of the elements of array could be handled by a single event handler. In VB.net, sadly, it isn't as easy as copy, paste, paste, paste. In this article, I wish to strate how to duplicate - to an extent - the functionality of a control array in VB.net.
First of all, there are two advantages to a control array.
1. You can easily access and change every element of the array with a
for loop. For example, lets say you have 10 textboxes on the form and you
want to add the values within them up, it would take less code to write: For i = 1 to 10 do
Sum += CInt(Text(i))
Next i
than to write Sum = Text1 + Text2 + Text3 ... + Text9 + Text10
.
2. You can make one event handler handle all their events. For example,
you have a bunch of buttons on your form resembling a keyboard (An On-Screen
keyboard), the code handling their OnClick event is very similar - each
button differing on the charachter that is displayed in a textbox. Wouldn't
it be easier to have one Event that can handle all there OnClick events and
would display a different charachter based on the button that prompted the
event? That is what can be achieved through Control Arrays.There are two ways to achieve this: There is a control by Eric Porter that allows you to add different controls to a control array or, you can do it in the following manner:
1) Create an array of the control that you wish to use.
Eg.
Dim Labels() as Label2) Create a procedure to make and add more controls to this array.
Eg.
Procedure MoreLabels(NewX as Integer, NewY As Integer) 'Add an object to the array ReDim Preserve Labels(Labels.Upperbound(0)) 'Initiate the object (Remember, the upperbound of our array has increased Therefore, "-1") Labels(Labels.Upperbound(0)-1) = New Label 'Add the control to the forms collection Me.Controls.Add(Labels(Labels.Upperbound(0)-1)) 'Use the AddHandler keyword to direct the handler of every element in the 'array to the same handler to achieve the "One event handles all" effect. 'ie. Instead of having lbl1_Click and lbl2_Click to handle each individual controls 'events, have Labels_Click which will handle Lbl1's and Lbl2's OnClick event AddHandler Labels(Labels.Upperbound(0)-1).Click, AddressOf Labels_click 'Add other EventHandlers over here. 'Set the name for later use - Not really necessary, but I use it for my "GetIndex" Function. Labels(Labels.Upperbound(0)-1).Name = "Lbl(" & Labels.Upperbound(0)-1 & ")" 'Set the controls location - Replace NewX and NewY with suitable values Labels(Labels.Upperbound(0)-1).Location = New System.Drawing.Point(NewX,NewY) 'Set other properties of the label .... End Sub
3) Add the events themselves.
Eg.
Private Sub Labels_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 'See below for GetIndex code. MsgBox("Hello there from: " & GetIndex(Sender)) End Sub 'This function returns the "Index" of an array. Ie. If Sender is Lbl(5) it 'will return 5 (See Labels_Click for Example) Function GetIndex(ByVal Sender As System.Object) As Integer 'I used "Pos" and "Name just to enhance readability Dim Name As String = DirectCast(Sender, Control).Name Dim Pos as integer = InStr(1, Name, "(") + 1 Return CInt(Mid(Name, Pos, Len(Name) - Pos)) End Function
And we have now acheived dynamically created, control array enabled controls on the form!
One last point regarding handling events: Lets say that you already have a few controls on the form that share a common event type (Eg. On_Click - both an editbox and a button have it...) and you want to make them share an event, all you need to do is just use the AddHandler keyword.
Relman
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
