Hi
I'm a bit new to custom events and delegates so I'm having problem to get a good solution to this problem.
I've got an MDI app that can load several forms as child-forms, all with different contents.
It can also show a .showdialog form for input of new data and on exiting this form the MDI form needs to notify one of the loaded Child forms that it's data has been changed and its time to reload the list content.
So, How can I raise an Event in one loaded Child form from the MDI form?
I can use something similar to this function to find out IF the child form is loaded or not.
(I'm using this function to avoid loading the same child form twice)
public static bool ShowThisChildWindow(String WinTagName)
{
foreach (Form childForm in Application.OpenForms)
{
if (childForm.Tag != null)
{
if (childForm.Tag.ToString() == WinTagName.ToString())
{
childForm.BringToFront();
return true;
}
}
}
return false;
}
Best Regards
Kjell
Loading
VulpesPosted Apr 19, 2011, 2:29 PM
Kjell LiljegrenPosted Apr 20, 2011, 5:39 AM
this is what I was looking for.
Together with an article I found in another forum I managed to solve this problem by using a custom event.
For anyone still having problem like I did to understand the basics of a custom event, this article helped me:
http://www.dreamincode.net/forums/topic/176796-quick-and-easy-custom-events/
To sumarize my solution based on the input
Create the Event handler in the MDI-form
-when instanciating a MDI-child let that new Child subscribe on the 'Event'
You can then from the MDI raise the Event and the declared event function in the MDI-child will set off
Rgds
Kjell
Sam HobbsPosted Apr 19, 2011, 3:50 PM
MDI forms might be the most convenient solution but it is likely there are more appropriate solutions. If MDI is the most convenient solution then I will try to help you but I suspect that Vulpes will provide the assistance you want. Note that in my opinion a more object-oriented solution would make the application less complex.
Kjell LiljegrenPosted Apr 19, 2011, 1:08 PM
firstly, I do not think I have misunderstand the concept of MDI versus not MDI (SDI), i think its more of a language barrier, My child forms can be called documents, I mean; what is a document, for some its a paper on the desk, for other its a file, and in this case it can be a form.
So, I have a MDI type of window application, this app has some "modules" represented as Child forms, meaning that the MDI-form is the MDIparent etc, I hope this is in line with a MDI-application.
Each of these child forms can only appear in one instance, for controling this I gave each a unique TAG-property, so if you try to load a second, the for-mentioned function will find the first instance and bring that to the front for the user.
One of these child forms has a list of "objects" and to add more of these "objects" I created a dialog form, I dialog form for me is a .Showdialog type of form and this will force the user to respond to that dialog in order to proceed.
and this is where my problem arises, when the dialog finishes I can evaluate what took place in the dialog, did the user Cancel it or fill it with information....
If it got filled I have a SP (Stored Procedure) that will create the new "object" and now we need to highlight to the Child form holding the list that it is one object short.
This can be achived in many ways (i hope) but I would like, if its possible, to find a mechanism so i can raise an Event within that form so the Child Form itself re-runs the routine that fills the list with objects.
The routine is already in place, the user can either hit F5 or pull down a menu and choose "Update List content"
So, My problem is that the Function calling the .showdialog is within the MDI form, hence the need to raise an event in another form, not the MDI-form, not the dialog form, a completely different form that might not even be loaded.
I can loop all loaded forms and find my form, but how can I raise an event within that form?
Kjell
VulpesPosted Apr 18, 2011, 4:59 PM
If all the child forms were subscribing to some event then, when the event was fired, they'd all be notified simultaneously which doesn't sound like what you want unless they need to know when something is happening on one of the other children.
Sam HobbsPosted Apr 18, 2011, 4:53 PM
Note that MDI means Multiple Document Interface. It is not designed for the purpose of showing multiple windows, it is designed for multiple documents and a UI for each document. The UI includes a window and a menu.
So when you say "the MDI form needs to notify one of the loaded Child forms that it's data has been changed" it sounds like you have data (called a document) corresponding with each child window. That is good. Hopefully it is just a matter of designing your application to be consistent with the way that MDI is designed to be used. So normally, each child would process a request (show a dialog) for new data. The data should be stored in an object (the document) and then the window should be updated from the document. Does that sound relevant to your application?