We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

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

rangers99
rangers99 asked
on
Medium Priority
386 Views
Last Modified: 2012-05-06
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.
Comment
Watch Question

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?

Author

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;
                        }
                    }
                }
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.

Author

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?
Hmm not sure, sounds likeley but maybe there are some weird sceanrios where this wouldn't be the case?
Hi Dave

Ive solved it.

'ContainsFocus' works. Ive no idea what Focused does. Its a bit confusing.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
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
Ok cool as long as it's working that's the main thing.

Commented:
try


if(this.ActiveMdiChild == childForm)
{
     //Your work
}

Author

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.
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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.