Sign in to follow this  
Faraday

[.net] [C#] How does Child influence Parent?

Recommended Posts

Hello all. Any help appreciated. I have an MDI app in VC#. I have 2 questions. 1. I want a menu, which is attached to Parent Form, to enable/disable when a Child Form gains/loses focus as well as upon destruction of the Child. How do I do this? For example, I would like the menu to change in response to OnClosed or OnClosing of a Child. I have contemplated making the relevant ToolStripMenuItem accessible to the Child, but I can't escape this feeling that such method violates OOP philosophy. Logically I would think that the Parent is aware of what's going on within the Child, but I can't figure it out. 2. How do I identify (within the Parent) a random Child when I don't know the order in which they were index'd?

Share this post


Link to post
Share on other sites
Have the parent subscribe to the child's OnClosing event.

For number two can you explain more what you are trying to do. I don't understand.

theTroll

Share this post


Link to post
Share on other sites
Quote:
Original post by TheTroll
Have the parent subscribe to the child's OnClosing event.
Does that mean capture the windows message upon child's closure? At the moment my workaround has been to make a public method in Parent class that returns a reference to the ToolStripMenuItem object... I don't know if that's a healthy way to do this?

As for question 2, say if I have a MDI parent window, which has 2 children - a graph window and list window (or several child windows). When a user closes one of them down, I would like to know which one. I know there is an array MdiChildren[] in the Parent class, but as much as I can see, I would need to know the order in which child windows were created to make use of that (or am I wrong?).


Share this post


Link to post
Share on other sites
If you attach to the FormClosing event of the child, you should notice that the first parameter to your event handler is "Object sender" which should be the child form the user is trying to close. An example of the event handler is here.

Maybe you know this, but to have the parent subscribe to the child event, you would do something like this:

ChildForm frm = new ChildForm();
this.MdiChildren.Add(frm);
frm.FormClosing += new FormClosingEventHandler(frm_FormClosing)


I'm rather rusty when it comes to working with MDI forms, so my syntax might not be correct. But basically after you create the child window you want to attach to the child's FormClosing (or FormClosed) event. (I just put this in here because it's not clear to me whether your are familiar with how to do subscribe to events in C#. If you know how, just disregard this part)

Share this post


Link to post
Share on other sites
as per #2 You could do it in a number of ways. The first that comes to mind is subclassing the form - adding an Int32 or whatnot ID variables - and have a factory pattern that instantiates and keeps track of the forms. There's probably better ways though.

Share this post


Link to post
Share on other sites
kanato, I get the idea and that's enough for me to look everything else up. Thanks a lot.

SnOrfys, using ID does work as you suggested. I'm comfortable with this as I can visualize it and follow it cleanly so I'm satisfied :)

Thanks to all those who participated. Very much appreciated.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this