2

我需要在线程末尾设置一个按钮焦点。Button.Focus() 方法似乎不起作用。

例如:

Button1_Click(object sender, EventArgs e)
{
   Thread myThread = new Thread(theThread);
   myThread.Start();
}

theThread()
{
  ... 
  Button2.Focus(); // does not seem to focus the button
}

但是,如果我将 Button2.Focus() 放在 Button1_Click 中,它将聚焦,但对于我的项目,我不能这样做。

4

2 回答 2

3

有关此类问题的通用解决方案,请查看SyncronizationContextclass。但是,对于 Windows 窗体,您可以使用该Invoke方法,在 WPF 中,您可以使用Dispatcher.Invoke

//WinForms:
Invoke(delegate{ Button2.Focus(); });
于 2009-05-04T17:50:17.860 回答
1

任何 UI 更改都必须从表单的主线程进行。考虑从您自己的线程中调用表单的“Invoke”方法。您需要将“Invoke”委托传递给在您的按钮上调用“Focus”方法的方法。

于 2009-05-04T17:50:12.247 回答