|
-
Dec 7th, 2001, 05:38 AM
#1
Thread Starter
Hyperactive Member
Array of Controls
in VB6 all controls had an index property - that allowed an array of the control to be created. This was useful as it allowed controls to be created dynamically at run time and for the same code to be assigned to the whole array of any of the control events.
In Vb Net there is no index property (or if there is I havent seen it), does anyone know how the equivelent would be achieved in .NET?
-
Dec 8th, 2001, 05:37 AM
#2
Lively Member
You're right, the index property is gone.
But .NET has a lot better mechanisme.
Just create a new control (object) and set its size and location properties. Then you have to put it on the form by adding the control to the Controls collection.
Form.Controls.Add(YourControl)
Because the event model is completally based on delegates it's easy to have the same Event Handler for various Controls.
-
Dec 10th, 2001, 04:55 AM
#3
Thread Starter
Hyperactive Member
Thanks for the reply gijsj.
I'm not convinced about it being a btter mechanism though. For example, if I wanted to dynamically create an array of labels on the screen that all had a click event that changed the colour of the label (i'm not sure why I would want to either - but thats not the point) - in vb6 I could create the first label with an index of 0, and code the click event to change the backcolor property for the passed index. At run time I could just add controls using the load command - and the event was already coded for,
I appreciate in .Net that you can dynamically create objects, and I appreciate that you can have common code that is used for the event of more than one control, but how would I do this at runtime?
-
Dec 10th, 2001, 07:11 AM
#4
Lively Member
To create a control during Runtime and assign code to the event handler:
1. Put a label (label1) on a form, add the following code to the click event handler
MessageBox.Show(sender.ToString());
2. Put a button on the form, add the following code to the click event handler:
Label label2 = new Label();
label2.Text = "label2";
label2.Click += new System.EventHandler(this.label1_Click);
this.Controls.Add(label2);
I hope you can see the possibilities of using delegates now and that this mechanisme is a lot more powerful that old VB6 style.
Sorry that's in C#, personal preference :-)
-
Dec 10th, 2001, 07:17 AM
#5
Thread Starter
Hyperactive Member
ok - I see where you are coming from now.
Maybe not as simple as vb6, but a lot more powerful in what can be achieved...
-
Dec 10th, 2001, 07:23 AM
#6
Lively Member
Correct !!
One line of extra code, but a lot more power.
And that's what we wanted in VB right, more power :-)
-
Dec 12th, 2001, 11:33 PM
#7
Will that only work on dynamically created controls?
What would be the VB.NET version of that?
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
|