Wiring up events in a less-messy way

B

Brandon Potter

I'm frustrated with the way I've been handling events previously inside
WinForms apps. It always seems that I end up with the effect I call
"AddHandler Hell".

I would like to do things a little differently and try to apply a little
more object-orientation to events; for a low-level example, I would like to
have my main MDI form and some child forms enable and disable some buttons
and controls based on the user's login status (logged in or logged out).

What's the most efficient way to almost define an event globally, so that
when my "UserAuthentication" class raises an event, say,
"LoginStateChanged", that the main form and child forms can respond to it
without having to individually wire up and keep track of each window?

It almost seems like I should be able to define a central shared/static
class that holds most application-wide events (i.e. an AppEvents class), and
allow my UserAuthentication class to raise AppEvents's "LoginStateChanged"
event.

Can someone shed some light on this for me?

Brandon
 
C

Cindy Winegarden

In news: (e-mail address removed),
Brandon Potter said:
...for a low-level example, I
would like to have my main MDI form and some child forms enable and
disable some buttons and controls based on the user's login status
(logged in or logged out).

Hi Brandon,

I've done something like this in Visual FoxPro (now porting it to VB). The
base form has a method called ChangeMode() where it goes through all the
controls on the form and enables/disables them as appropriate. There's code
in the Add-Edit-Cancel button base classes that calls the form's
ChangeMode(). Each base class has a custom property that tells when it
should be enabled. The values are something like "ADD,EDIT" and ChangeMode()
parses the string to see what to do.

Note - I have a "base" form class that all my other forms inherit from, and
also "base" form controls that I use instead of the ones that come "in the
box."

This isn't exactly what you're trying to do but I'm sure it will give you
some ideas.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top