In my appplication I am trying to focus a textbox so I can type straight away after the Form is loaded.
When the Form is shown, I can see is the cursor blinking in the TextBox but if I type something nothing happens.
I need to click the Window to start entering text in the TextBox. If I run my application normally from Visual Studio, it will work perfectly, but if my application is run using the Task Scheduler, then this happens.
Do you have any advice?
Below is my code:
this.TopMost = true;
textbox.Focus();
I also tried textbox.Select(); but it doesn't work anyway.
The problem: when the application is run by a Task Scheduler Action, the main Window is shown non-active and the System notifies the User flashing the application's icon in the Task Bar. This is by design.
A simple workaround is to set the startup Window's
WindowState=FormWindowState.Minimizedin the Form Designer, then set it back toFormWindowState.Normalafter the Window has completed loading its content and it's ready to be presented, raising the Shown event.Setting
FormWindowState.Normalcauses a call to ShowWindow withnCmdShowset toSW_SHOWNORMAL:The Window is now shown as usual, active and ready to receive input.
Also, the code sets explicitly the Control that should receive the input, using the ActiveControl property.
I suggested to make the
Shownhandlerasyncand add a small delay before re-setting theWindowStateproperty, to prevent the Task Bar icon from getting stuck in a blinking state.If the Window needs to be repositioned or resized, this needs to be done after the
WindowStatehas been reset, since the Window is in a minimized state before that and won't cache position an size values.The Form's StartPosition should be set to FormStartPosition.Manual