[AccessD] OT: Modify the default event handlers to mimic the behavior of VB 6 control arrays (VB .NET)

Gustav Brock gustav at cactus.dk
Tue Sep 14 03:14:34 CDT 2004


Hi all .Net freaks

Just noticed this tip from Element K Journals.
But is it just me or would this be considered bad programming
practice? Whenever I find out an event for one control can be reused
by another control, I move that code to a subfunction.

/gustav

---

If you've been mourning the loss of control arrays in VB .NET,
wipe your eyes and listen up. While it's true that .NET put the
control Index property out to pasture, you can still point
multiple controls to an event subroutine just by modifying the
event subroutines .NET exposes automatically when you add a
control to a form. For example, suppose you have two buttons on a
form named Button1 and Button2. You can make the Click() event
subroutine for Button1 also handle the Click() event for Button2
just by adding Button2.Click to the Handles portion of the
subroutine, like so:

        Private Sub Button1_Click(ByVal sender As _ Object, ByVal e As
System.EventArgs) _
                Handles Button1.Click, Button2.Click

You can append as many control Click() events as you'd like to
the Handles statement, as long as you separate each event with a
comma. As an added bonus, you can also include other Button
events or even events from any number of non-button control types
on the form. That's more than you could ever do with a VB 6
control array.




More information about the AccessD mailing list