• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 372
  • Last Modified:

How Do I check if an MDI Child window has Focus?

I thought you could just use the Focused property ie.

if (win1.Focused)
{
  // do something
}

But this aint working. Whats the right method to use? Thanks.
0
rangers99
Asked:
rangers99
1 Solution
 
daveamourCommented:
The following works for me.

Myform t = new MForm();
t.MdiParent = this;
t.Show();
MessageBox.Show(t.Focused.ToString());
This is in the code for my Parent Form.  Where is the code you are running?
0
 
rangers99Author Commented:
Hi Dave
What Im doing is selecting a window then clicking a menu item which has the following code. Ive traced  it through and the Focused property is never set to true. Its always false. Id have thought clickin on a child window would have set the focused property.


 for (int i = 0; i < this.MdiChildren.Length; i++) {
                    if (this.MdiChildren[i] is TRResultWindow) {
                        if (this.MdiChildren[i].Focused) {

                            ((TRResultWindow)this.MdiChildren[i]).addResults(copiedResults);
                            i = this.MdiChildren.Length;
                        }
                    }
                }
0
 
daveamourCommented:
Hmm so if the Focused property isn;t working mayeb you can manually set it when the form received focus unless it is read only in which case you couuld add your own property and set that when the window receives focus and unset it when the window loses focus?
A bit hacky but might be worth a go.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
rangers99Author Commented:
Dave
Yeah it looks like the focused property is broken. Still I cant beleive I have to do a hack for something as fundamental as this. I see there is a TopMost property. This may work but is it automatically true that a window that has been clicked on by the user is the topmost window?
0
 
daveamourCommented:
Hmm not sure, sounds likeley but maybe there are some weird sceanrios where this wouldn't be the case?
0
 
rangers99Author Commented:
Hi Dave

Ive solved it.

'ContainsFocus' works. Ive no idea what Focused does. Its a bit confusing.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Try it this way?

    if (this.ActiveMdiChild is TRResultWindow)
    {
        // do something in here...
    }

See: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.activemdichild.aspx
0
 
daveamourCommented:
Ok cool as long as it's working that's the main thing.
0
 
prpradipCommented:
try


if(this.ActiveMdiChild == childForm)
{
     //Your work
}
0
 
rangers99Author Commented:
Hi guys

Thanks for the extra comments but I got it to work fine using the "ContainsFocus" property.
Im not sure what the procedure is to close a question, it would save you time trying to solve a problem for me that Ive already solved.
0
 
daveamourCommented:
If youve fixed a question yourself you should click delete question button at the top and then I think you have an option to accept your own answer as the solution.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now