Click here to Skip to main content
Click here to Skip to main content

Tagged as

How to prevent Form from loosing focus (in the application)

, 24 May 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
We all know that .ShowDialog() is great but sometimes You want to show user some window and keep program running. (.ShowDialog() freezes code execution in method as we all know) Let's say You have this scenario that You want to have form that will not loose focus but at the same time You...
We all know that .ShowDialog() is great but sometimes You want to show user some window and keep program running. (.ShowDialog() freezes code execution in method as we all know)
 
Let's say You have this scenario that You want to have form that will not loose focus but at the same time You want to do some work after window is shown. Of course .ShowDialog() is not an option because work after it will not be done before You close the window. So how can You acomplish this ?
 
I've searched on forums and some solution (especially with Deactivate/Activate) simply don't work. So how can You do this ?
 
In Your form class, override OnLostFocus(EventArgs e) and place focusing call inside it. So it looks like this:
protected override void OnLostFocus(EventArgs e)
{
        base.OnLostFocus(e);
        this.Focus();
}
 
Simple but works. Smile | :)
 

OK. In some cases (and i don't know exactly why) you should do
 
protected override void OnDeactivate(EventArgs e)
{
        base.OnDeactivate(e);
        this.Focus();
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Adrian Pasik
Software Developer Agilion Consulting
Poland Poland
I specialize at C#, developing Enterprise solutions. I have some knowledge of ASP.NET MVC - looking forward to use it together with Typescript.
Follow on   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.141015.1 | Last Updated 24 May 2010
Article Copyright 2010 by Adrian Pasik
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid