Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

Index property in .net

I used to program in VB6 - and if I wanted an array of buttons, I could use the index property when I put the buttons on the form.  I have noticed that .net controls don't have this property - so how do I put an array of buttons on a form?

Marvin Schwartz
Saturday, September 21, 2002

As you've discovered, there is no fixed notion of "control arrays" in VB .NET. However, there are ways to do most everything that you could do with a control array. The real question is what are you trying to do? Handle events from multiple controls in a single place, create controls dynamically, or what?

Mike Gunderloy
Saturday, September 21, 2002

I am trying to handle events from multiple controls in a single place.

Marvin Schwartz
Sunday, September 22, 2002

That's easy to do - just attach the event handlers for each button to the same method in the form class.

The other option would be to derive a new control from the one you want to use, and place the common event handling in the derived class. That way you don't have to hook up all those delegates.

Chris Tavares
Sunday, September 22, 2002

"...just attach the event handlers for each button to the same method in the form class."

...and to find out which button was clicked look in the "sender" parameter that bets passed to you.

Dunc
Sunday, September 22, 2002

??  should be: "that *is* passed to you"

Dunc
Sunday, September 22, 2002

And just to add a tiny bit more detail -

There are two ways that you can attach the same event handler to multiple events. If you know at design time what plumbing you need, you can simply list all of the events in the Handles clause of the event handler declaration. If you don't know until run-time, you can use the AddHandler statement to hook up any event handler to any event dynamicaly.

Mike Gunderloy
Sunday, September 22, 2002

*  Recent Topics

*  Fog Creek Home