Controls cannot be focused unless their owner window is visible. The window is not visible in the Load event or the class constructor.
Here's an example of one method to get around this issue...
Code:
namespace YourApplication
{
class YourWindow : Form
{
public YourWindow()
{
InitializeComponent();
this.HandleCreated += new EventHandler(YourWindow_HandleCreated);
}
void InitializeComponenet()
{
// ... stuff
}
void YourWindow_HandleCreated(object sender, EventArgs e)
{
this.BeginInvoke(new MethodInvoker(FocusFirst));
}
void FocusFirst()
{
this.SomeControl.Focus();
}
}
}
I use the Control.BeginInvoke() method, which can only be called after the window's handle has been created. The FocusFirst() method is called, and any control can be forced to focus, basically