How to tell parent window when a child window is clilcked on?

  • Thread starter Richard Lewis Haggard
  • Start date
R

Richard Lewis Haggard

What is the mechanism by which a child window can notify its parent that it
has been clicked on?
 
B

Bruce Wood

Richard said:
What is the mechanism by which a child window can notify its parent that it
has been clicked on?

Can't you just have the parent form listen to the child form's Click
event?

If you need more specific behaviour, you could write a new event into
the child form and have the parent form subscribe to that event.
 
R

Richard Lewis Haggard

Thank you, but restating the question does not actually provide any useful
information that will help solve the problem. I need to know how to set up
things so that a child window can notify its parent of an event in a child.
 
B

Bruce Wood

Richard said:
Thank you, but restating the question does not actually provide any useful
information that will help solve the problem. I need to know how to set up
things so that a child window can notify its parent of an event in a child.

What part of "...have the parent form subscribe to that event" was
unclear?

If you are looking for code, this is what I meant:

public class ParentForm : Form
{
...
ChildForm child = new ChildForm();
child.Click += new System.EventHandler(child_Click);
child.Show();
...

private void child_Click(object sender, System.EventArgs e)
{
MessageBox.Show("The child was clicked!");
}
}

Of course, this event will occur only when the user clicks on the child
form itself and not on any of the controls contained within the form.
(Clicks on controls within the child form will, of course, be handled
by those controls and will not be seen as a Click on the form.)

If this isn't what you want, then perhaps the problem is in how you
stated your question. "Click on the child form" could mean precisely
that: a click event on the child form, in which case the above code is
what you need. Or, perhaps you meant "when the user gives the child
form focus" in which case the GotFocus event might be more appropriate.

It may also make a difference whether you are programming WinForms
(which is what I do) or WebForms (I can't speak to the subtleties of
Web programming).

As well, what you mean by "when the user clicks on the child form" may
be when the user clicks *anywhere* within the bounds of child form,
even on a control, which is a whole other kettle of fish.

The problem, from my side of the Web, is that your question is rather
vague. The child form doesn't "notify the parent of an event in the
child". The parent, rather, subscribes to one of the child's events,
and so registers its interest in something that may happen within the
child form. Are you having trouble in the parent, subscribing to
events? Or do none of the standard events for a Form meet your needs?
Or perhaps you're subscribing but your handler is never called? Or
perhaps you're using the word "event" in a general (English) sense
rather than in the technical sense of a C# event?

Rather than tossing back ironic comments, perhaps providing more
information about exactly what it is you're trying to do would get you
a better answer.
 
R

Richard Lewis Haggard

Ah! Now I understand what you meant. I knew that there must some easy way of
doing it but couldn't figure out the right keys to push in order to
accomplish the desired result. Thanks!
 

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