How to inform parent window the control that has focus?

D

Deena

Hello

Is there anyway that a parent form can be informed if a control on it has
focus? I could just inform the parent form on the controls "GotFocus" event
but this is not the ideal solution for me. It would mean adding code for
every control that I add to the form. And I want to create a base dialog
which will have the same behaviour througout my application. I.e. I can add
as many controls as I want and my form will always be aware which control
has focus.

Eg: I click on a text box "txtName" on a form. The form the control is on
immediately knows that "txtName" has focus....(then process that info)

Thanks in advance.
Deena.
 
G

Guest

Haven't tried it out.
But because you want the Parent form to do something when a control has focus.. then you need an event. GotFocus seems like the only choice. You can create one Control.GotFocus event that is used for all controls so all the code is in one spot.
Then switch the sender to see which one has focus.

Just a thought.
 
D

Deena

But that would still mean adding code for each control wouldn't it? I don't
want to do that. I would like for a form to be aware if it's control has
focus or not. So I can add drag and drop as many controls as I want - and it
would be able to determine if it's child control has focus. I'm beginning to
suspect this might not be possible :(



KeithH said:
Haven't tried it out.
But because you want the Parent form to do something when a control has
focus.. then you need an event. GotFocus seems like the only choice. You can
create one Control.GotFocus event that is used for all controls so all the
code is in one spot.
 
D

Deena

public FormFocusControl()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();

AddFocusEvent(this);

}



private void AddFocusEvent(Control parentControl)

{

foreach (Control control in parentControl.Controls)

{

if (control.Controls.Count == 0)

{

control.GotFocus += new EventHandler(GotFocus_EventHandler);

}

else

{

AddFocusEvent(control);

}

}

}

private void GotFocus_EventHandler(object sender, EventArgs e)

{

//TODO:

}
 

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