Click here to Skip to main content
15,908,909 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have created an application which will keep on working in background and not visible to user. When user presses a hot key then I will prompt for password.

I have made my form’s

ShowInTaskbar=false and set is a model.

Now the problem is when I will show it again, it will take time of a sec or two to get focus. It will be visible but will not get focus immediately.

I have tried to set focus manually to textbox but it does not work for me.

Any help will be appreciated.
Posted
Comments
Sergey Alexandrovich Kryukov 21-Jun-11 12:28pm    
"Model" or "modal"?
--SA

If application is activated, it is activated and got focus immediately, unless you screw up something, such are rendering of control. Question is not clearly formulating, because there three related aspects of showing application and focusing: 1) putting a form on top in Z-order, see System.Windows.Forms.Form.BringToFront; 2) activation, see System.Windows.Forms.Form.Activate; 3) getting keyboard focus; if the form has any controls (it should have, otherwise why would you need one :-)), focusing of the form makes no sense; one of the controls should be focused, see System.Windows.Forms.Control.Focus. If some control was focused before deactivation, it will be focused again on activation; you don't have to do it in code. Besides, you don't want loosing a change in focus the user have done before deactivation.

See:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.aspx[^].

You can investigate why it goes so slowly; you can research order of operations and time them handling events System.Windows.Forms.Form.Activated and System.Windows.Forms.Control using System.Diagnostics.Stopwatch, http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx[^]. It's hard to tell you more without looking at your code.

Why do you use ShowInTaskbar = false? Is does not make sense for main form of the application (but very useful for all other forms which should better be Owned). However, if this is a tray icon application, this is right thing to do.

—SA
 
Share this answer
 
v2
Comments
Abhinav S 21-Jun-11 13:23pm    
Accurate answer. My 5.
Sergey Alexandrovich Kryukov 21-Jun-11 14:16pm    
Thank you, Abhinav.
--SA
Sergey Alexandrovich Kryukov 21-Jun-11 14:19pm    
First paragraph appended with important note on focus position after activation.
--SA
Make sure you don't do any time consuming processing every time a form activates.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 21-Jun-11 14:17pm    
It's hard to say what causes delays; could be many different reasons.
--SA

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900