The application was designed so that there was a main form (called frmMainForm, naturally) which contained all common elements such as menu, toolbar, status bar, etc. Within this main form was panel in which other forms were loaded based on user selection. What was required was that a toolbar button be enabled/disabled based on a certain state in a child form, and for a value (in this case, an "applicationID") be available when the toolbar button was clicked.
I decided the most elegant way to accomplish this was to create a custom event in the main form that could be called whenever the application ID was changed in the child form. The first thing I did was declare a delegate in the main form:
public delegate void ChangedEventHandler(object sender, ApplicationEventArgs e);Note the second argument in the delegate, the "ApplicationEventArgs" object - I'll describe this class shortly. The next thing I did was to declare a public event of type "ChangedEventHandler". This is the delegate object we previously declared:
public event ChangedEventHandler ApplicationIDChangedEvent;The next thing to do is to create the actual event handler. By convention this always seems to be named On EventName. In this case I called it:
public virtual void OnApplicationIDChanged(ApplicationEventArgs e)
{
ApplicationIDChangedEvent(this, e);
}
Finally I created a method that will be called when the event is fired. This does the required
processing that we wanted to accomplish:
private void ApplicationID_Changed(Object sender, ApplicationEventArgs e)
{
this.applicationID = e.ApplicationID;
if (this.applicationID == 0)
this.btnViewApp.Enabled = false;
else
this.btnViewApp.Enabled = true;
}
The last thing we need in the main form to make the whole thing work is to wire up the above method
to the event that we created, so that when the event is fired, this method is called. we do this in the
constructor of the form, this way we're sure that the event handler association is created. It looks
like this:
this.ApplicationIDChangedEvent += new ChangedEventHandler(this.ApplicationID_Changed);As you can see, it takes the ApplicationIDChangedEvent and adds a new ChangedEventHandler, which takes as an argument the ApplicationID_Changed method. The ApplicationID_Changed method signature will match the delegate that we previously defined.
The next thing to cover (as promised) is the ApplicationEventArgs object. This is basically a class that extends the EventArgs class. In it's constructor it accepts an integer parameter (applicationID) and exposes this value as a public property. It's quite self explanatory:
public class ApplicationEventArgs : EventArgs
{
private int applicationID;
public ApplicationEventArgs(int applicationID)
{
this.applicationID = applicationID;
}
public int ApplicationID
{
get { return this.applicationID; }
}
}
The very last thing to is to USE the event that we went through all this work to create. In the child form we simply
call this event when required. In this particular scenario, we want to notify the main form when the application ID
has changed. This is accomplished in the child form's event handler for the "MouseUp" event of a datagrid that
displays a list of applications. So, when the user selects a row we call the event. Note that in the child form
we have a reference to the main form. It's simple to fire the event:
mainForm.OnApplicationIDChanged(new ApplicationEventArgs(applicationID));Note that here we're using the ApplicationEventArgs object, allowing us to pass in the applicationID value. The neat thing is that we can notify the main form that we want to disable the main form's toolbar button by firing the event with an applicationID of zero:
mainForm.OnApplicationIDChanged(new ApplicationEventArgs(0));