[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 358
  • 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

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