WPF 如何取消TabControl自带的左右键切换TabItme的功能?

禁用系统左右光标键的切换,重点是: e.Handled = true; 这句话。

        private void tabControl1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Left || e.KeyCode == Keys.Right)
            {
                e.Handled = true;
            }
        }

 

上面这段代码就是禁用了系统的左右光标键的切换。

 

2、下面这段代码是实现用光标键控制选项卡循环切换。

private void tabControl1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Left || e.KeyCode == Keys.Right)
            {
                e.Handled = true;
            }

            int nextIdx = tabControl1.SelectedIndex;

            if (e.KeyCode == Keys.Up ||  e.KeyCode == Keys.Left)
            {

                if (tabControl1.SelectedIndex <= 0)
                {
                    nextIdx = tabControl1.TabCount - 1;
                }
                else
                {
                    nextIdx = nextIdx - 1;
                }
            }
            else if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Right)
            {

                if (tabControl1.SelectedIndex >= tabControl1.TabCount - 1)
                {
                    nextIdx = 0;
                }
                else
                {
                    nextIdx = nextIdx + 1;
                }
            }

            tabControl1.SelectedTab = tabControl1.TabPages[nextIdx];

        }

  

posted @ 2022-03-29 20:41  奥兰王子  阅读(608)  评论(0编辑  收藏  举报