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

Gustav Brock gustav at cactus.dk
Tue Sep 14 09:55:40 CDT 2004


Hi Brett and John

Brett, thanks for clearing this up. I can see I have been victimised
by unreadable code as I - as you guessed - wearing my Access goggles
read Button1_Click as the event handler.

So this in the "reverse" of calling a sub from several events?
You specify a sub and specify - withing this - which events should
call the sub? Have in mind please, that I'm not familiar with neither
VB6 or dot Net.

Of course you are both right about WithEvents. However, sometimes you
just need to, say, have updating of a checkbox and clicking a button
to do the same; then I don't write duplicate code but move it to a sub
which I call from the relevant events.

/gustav


> Looks OK to me.  Are you saying that if you had consistent behaviors for
> every button on your form, that you would write separate event handlers
> for each control to call another proc?  Ugh!

> BTW, you can call your event procedure anything you want.  Get rid of
> the Button1_ moniker and it would be much more readable:

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

> -----Original Message-----
> From: accessd-bounces at databaseadvisors.com
> [mailto:accessd-bounces at databaseadvisors.com] On Behalf Of Gustav Brock
> Sent: Tuesday, September 14, 2004 3:15 AM
> To: Access Developers discussion and problem solving
> Subject: [AccessD] OT: Modify the default event handlers to mimic
> thebehavior of VB 6 control arrays (VB .NET)


> 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