在 windows 表单上,将选项卡焦点设置为按钮

On windows forms, set tab focus to a button

我创建了一个 Outlook 添加,其中有时会显示一个 windows 表单,上面有四个按钮。我正在尝试将焦点默认设置为第一个按钮,但是当我将此按钮默认为开始时的焦点按钮时,视觉 "selected" 边框不会出现在按钮周围。

有什么办法可以实现吗?

您可以使用这些选项之一将焦点设置在 Load 窗体事件中的控件上:

  • this.ActiveControl = this.button1;
  • this.button1.Select();
  • this.Show(); this.button1.Focus();.

只有在窗体的Visible 属性设置为[=后,才能在窗体的Load事件中使用Control.Focus方法将焦点设置在控件上18=].

在 select 按钮之后,按钮的边框将以显示它是活动控件的方式绘制,但不会绘制焦点提示。

作为一种快速而肮脏的修复方法,您可以发送一个 Tab 和一个 Shift + Tab 到您的表单:

SendKeys.SendWait("{TAB}");
SendKeys.SendWait("+{TAB}");

如果您有兴趣更改 Button 的标准行为以在代码中按下 select 按钮或使用鼠标时查看焦点提示,您可以创建自己的继承 Button 的按钮并将其 ShowFocusCues 覆盖为 return Focused 值。您可以阅读更多相关信息 :

public class MyCustomButton : Button
{
    protected override bool ShowFocusCues
    {
        get { return this.Focused; }
    }
}