|
-
Nov 19th, 2006, 10:25 PM
#1
Thread Starter
Member
[RESOLVED] VB and control arrays.
I know control arrays are gone. I don't like that fact, but am choosing to live with it and learn VB 2005. Anyway, I have a program from VB6 that I need to convert. It has 128 control array labels and 128 control array buttons.
I DO NOT want to create these controls at run time. They're already on the form.
1. Do I have to list each of the HANDLES separately, like ....HANDLES Label1.click, Label2.click, Label3.click, .... Label128.click?
2. I figure the easiest way to reference them by an index is to Dim MyLabel() as label = {me.Label1, me.Label2, ..., me.Label128}. Again, do I need to list all 128 individually or is there an easier way?
3. When I try to create a label array like this, even if I make it public and put it in the declarations section of the form, I still can't access it from each SUB procedure inside that form. Do I need to declare it inside each SUB?
Greg
-
Nov 19th, 2006, 11:03 PM
#2
Re: VB and control arrays.
If there are only those 128 labels on your form, you can loop trhu the form controls and check for the type of the control. If it is a label then add it to the array.
-
Nov 19th, 2006, 11:34 PM
#3
Re: VB and control arrays.
this is an example of looping through all controls on a form and adding the labels to an array
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim labels As Label()
Dim i As Integer = -1
Dim ctl As Control = Me.GetNextControl(Me, True)
Do Until ctl Is Nothing
If TypeOf (ctl) Is Label Then
i += 1
ReDim Preserve labels(i)
labels(i) = DirectCast(ctl, Label)
End If
ctl = Me.GetNextControl(ctl, True)
Loop
End Sub
-
Nov 20th, 2006, 04:48 AM
#4
Re: VB and control arrays.
bmahler, are you aware that using your code will create 128 distinct array objects? Use a collection or start with a bigger array and do NOT simply resize it one element at a time.
If you want to create one event handler for multiple controls then simply select all the controls in the designer, click the Events button in the Properties window, double click the desired event and voila: one method with every selected control in its Handles clause.
You declare the array at the class level and it is then available throughout the class, whether its public, private or whatever.
-
Nov 20th, 2006, 04:59 AM
#5
Addicted Member
Re: VB and control arrays.
 Originally Posted by ghall426
I know control arrays are gone. I don't like that fact, but am choosing to live with it and learn VB 2005. Anyway, I have a program from VB6 that I need to convert. It has 128 control array labels and 128 control array buttons.
I DO NOT want to create these controls at run time. They're already on the form.
1. Do I have to list each of the HANDLES separately, like ....HANDLES Label1.click, Label2.click, Label3.click, .... Label128.click?
2. I figure the easiest way to reference them by an index is to Dim MyLabel() as label = {me.Label1, me.Label2, ..., me.Label128}. Again, do I need to list all 128 individually or is there an easier way?
3. When I try to create a label array like this, even if I make it public and put it in the declarations section of the form, I still can't access it from each SUB procedure inside that form. Do I need to declare it inside each SUB?
Greg
Do EACH label has different event (ex. label1 will do A, label2 will do B) ? I assumed it's not, since you have control array there.
You should use AddHandler Method for your buttons. Iterate all controls in your form, and add a handler to each of them.
-
Nov 23rd, 2006, 02:01 AM
#6
Thread Starter
Member
Re: VB and control arrays.
 Originally Posted by jmcilhinney
You declare the array at the class level and it is then available throughout the class, whether its public, private or whatever.
So how can I make this work? I get "Object reference not set to an instance of an object."
Public Class Form1
Public MyArray() As Button = {Me.Button1, Me.Button2, Me.Button3}
Private Sub AnyButton_Click(.........) Handles Button1.Click, Button2.Click, Button3.Click
Debug.Print(MyArray(1).Text)
End Sub
End Class
-
Nov 23rd, 2006, 02:34 AM
#7
Re: VB and control arrays.
That's because when that array is created the Buttons don't exist, so you're populating your array with three null references. The Buttons aren't created until the InitializeComponent method is called in the constructor, which occurs after the declarations of the member variables. In short, you must declare the array at the class level but you cannot populate it until after the InitializeComponent method has been executed. That means writing your own constructor and populating the array at the end of that or, more likely, doing it in the Load event handler:
VB Code:
Private buttons As Button()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.buttons = New Button() {Me.Button1, Me.Button2, Me.Button3}
End Sub
-
Nov 23rd, 2006, 03:36 AM
#8
Thread Starter
Member
Re: [RESOLVED] VB and control arrays.
Thanks to all who responded and answered my questions. Great help as always. Keep it up.
Greg
-
Nov 23rd, 2006, 04:12 AM
#9
Re: [RESOLVED] VB and control arrays.
Also:
 Originally Posted by ghall426
I DO NOT want to create these controls at run time. They're already on the form.
Technicality, but they're actually still created at run-time. The Form Designer simply generates the code to create them, and hides it in the .designer file.
Enumerating the Controls collection at run-time just to add event handlers is, thus, somewhat perverse.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|