Mdi Call Methods in Child

barski

Junior Contributor
Joined
Apr 7, 2002
Messages
240
Location
Tennessee
I have a mdiparent that has a combobox named cmbStore that i want to use as a "filter" for the child forms. For example if i open the mdiChildSales form i only want it to show sales for the store currently selected. All is fine on the open of the child form because i created a static variable called SelStore so when the form opens it uses that variable as a parameter for the sales stored procedure but if i change the cmbStore selection while the mdiChildSales is open how does the mdiChildSales know SelStore value has changed.
 
Events :).

Well, that doesnt tell you much so I'll try to explain. On your mdi form you define an event (delegate) that is triggered when the filter has changed, and you use this delegate in an event fired from the mdi window. Something like this:

Code:
public delegate void FilterChangedEventHandler(string newFilter);
public event FilterChangedEventHandler FilterChanged;

Now, whenever somebody changes the filter, you fire that event.
Note that (at least in C#) there is one annoying thing about the event, it is null when nothing has subscribed yet so make sure you check for that. When the filter is changed use something like:

Code:
if (FilterChanged != null)
{
   FilterCanged(theNewFilter);
}

That is the sending part. Now you also have to change the mdi child windows, they will have to subscribe to this event. So make a method to let them subscribe to the event. In the mdi child window this could be :
Code:
public void RegisterMdiParent(Form mdiParent)
{
  mdiParent.FilterCanged += new FilterChangedEventHandler(FilterChangedHandler);
}

and offcourse, implement the method that is called: FilterChangedHandler.

Code:
public void FilterChangedHandler(string newFilter)
{
//do whatever needs to be done to apply the filter.
}

Off course you still need to keep the code you currently have to set the correct filter when the mdi child is shown.
 
thanks so much!!! that is exactly what I was looking.

the last thing i need to figure out is when the FilterChanges I do an "autosave" if the current collection haschanges and all works well but if there is an error let's say they didn't fill in a required field then i would want to cancel the cmbchange event and that isn't a cancelable event like the key press event so what to do?
 
upon further review i think maybe it would be better to implement an interface


like IMyChild and when the user requests another form to be open it calls the

IMyChild.FilterChanged

I think this would allow me to do things like implement the "savedata" better. If anyone disagrees please speak up. I'm about to redo my whole project so if there is an error in my thinking please point it out.
 
Back
Top