Question

I find a strange bug in windows 2012. I have a simple window (WinForm) with a text box and a button (textBox1 and button1).And I try to focus on textbox1, after form appear.

private void Find_Paint(object sender, PaintEventArgs e)
{
   textBox1.Focus();           
}

And if I set it Click and MouseClick events stop working. So I can't click on button. In windows 2008 it's work. If comment focus line - works too.

Who can suggest a solution or perhaps an alternative? Need to get the cursor in the textbox after the form has appeared

Était-ce utile?

La solution

You should use the Shown event instead:

private void Find_Shown(object sender, EventArgs e){
  textBox1.Focus();
}

Note: you used Paint event which will be very nasty, everytime your form is repainted, your textBox1 will be focused, the Paint event is fired every time your form resizes, state changes, ... we can't determine exactly the time it fires but it fires fairly frequently when your form is running. That is the reason why you can't click on and select anything on your form. That's because clicking or selecting controls fires the Paint event and makes your textBox1 focused then.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top