在TabControl中选择标签页而不会占用焦点 [英] Select Tab Page in TabControl without stealing focus

查看:61
本文介绍了在TabControl中选择标签页而不会占用焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 TabControl.SelectTab("...")显示选项卡,但同时也提供选项卡焦点.我想显示一个特定的选项卡,但请保持焦点.

Using TabControl.SelectTab("...") shows the tab but it also gives the tab focus. I would like to show a particular tab, but keep focus where it is.

我在网格中有数据行.根据所选行的属性,我显示了一个不同的标签页,以具有不同的UI布局.但是,当使用箭头键在各行中滚动时,焦点将切换到选定的选项卡-我不想发生这种情况.

I have data rows in a grid. Based on properties of the selected row, I show a different tab page to have a different UI layout. But when using arrow keys to scroll through rows, the focus switches to the selected tab -- which I don't want to happen.

谢谢.

推荐答案

我认为没有内置函数,但是您可以通过以下方式实现:

I don't think there's a built-in function, but you can do in this way:

private bool skipSelectionChanged = false;

private void dataGridView_SelectionChanged(object sender, EventArgs e)
{
    if (skipSelectionChanged)
        return;

    // supposing we decide tab[0] has to be selected...
    this.SelectTabWithoutFocus(this.tabControl1.TabPages[0]);
}

private void SelectTabWithoutFocus(TabPage tabPage)
{
    this.skipSelectionChanged = true;

    // "this" is the form in my case, so you get the current focused control
    // (ActiveControl), backup it, and re-set it after Tab activation

    var prevFocusedControl = this.ActiveControl;
    if (this.ActiveControl != null)
    {
        this.tabControl1.SelectedTab = tabPage;
        prevFocusedControl.Focus();
    }
    this.skipSelectionChanged = false;
}

在这里,我备份当前的焦点控件,选择所需的选项卡,最后将焦点设置为原始控件.

Here, I backup the current focused control, select the desired tab, and finally set the focus to the original control.

跳过布尔值是必要的,因为将焦点放在网格上会再次触发 SelectionChanged 事件,从而导致无限循环.

Skipping boolean is necessary, because giving the focus to the grid you trigger SelectionChanged event again, causing infinite looping.

这篇关于在TabControl中选择标签页而不会占用焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆