【问题标题】:Setting Focus on another control on button click在按钮单击时将焦点设置在另一个控件上
【发布时间】:2016-06-02 10:53:31
【问题描述】:

当我点击一个按钮时,它会保持选中状态 我可以做些什么来取消点击后的按钮?见下图:

【问题讨论】:

  • 这是 Windows 应用程序还是 Web 应用程序?
  • @PranavPatel Windows 应用程序
  • 试试this.ActiveControl = <Any control other than Start button";
  • 只有一种方式,点击后将焦点放在另一个控件上

标签: c# winforms button


【解决方案1】:

Focus Method

private void btnStart_Click(object sender, EventArgs e)
{
    btnStop.Focus(); //setting the focus on Stop Button

    // ...your code
}

ActiveControl Property

private void btnStart_Click(object sender, EventArgs e)
{
    this.ActiveControl = btnStop; //setting the focus on Stop Button

    // ...your code
}

SelectNextControl Method

private void btnStart_Click(object sender, EventArgs e)
{
    Control p;
    p = ((Button)sender).Parent;
    p.SelectNextControl(ActiveControl, true, true, true, true);

    // ...your code
}

【讨论】:

  • 我想补充一点。当通过 userCtrl.Focus() 将 UserControl 嵌入 TabControl 请求焦点时,总是返回 false 并且焦点不会转移。但是设置 this.ActiveControl 可以完美运行。
【解决方案2】:

您需要其他一些可聚焦的控件来将焦点移动到喜欢您的StopButton

你可以设置btnStop.Focus () ;

你也可以像这样将表单的activecontrol属性设置为null

 this.ActiveControl = null;

或者在设置顺序后使用 Tab 顺序:

SendKeys.Send("{TAB}");

【讨论】:

    猜你喜欢
    • 2010-12-01
    • 1970-01-01
    • 2010-11-25
    • 2017-01-02
    • 1970-01-01
    • 2014-11-22
    • 2011-01-04
    • 2016-10-22
    • 1970-01-01
    相关资源
    最近更新 更多