且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

单击选项卡单击键盘时如何将一个选项卡移动到选项卡控件中的另一个选项卡页?

更新时间:2022-10-23 12:52:00

您好Shankar,

您可以尝试以下代码(使用左侧右键更改选定的标签页):

 private void tabControl1_KeyPress(object sender,KeyPressEventArgs e)
{
TabControl tb = sender as TabControl;
if(tb.SelectedTab!= null)
{
switch(e.KeyChar)
{
case(char)Keys.Left:
if (tb.SelectedIndex> 0)
tb.SelectedIndex--;
休息;
case(char)Keys.Right:
if(tb.SelectedIndex< tb.TabPages.Count - 1)
tb.SelectedIndex ++;
休息;
默认值:
break;
}
}
}

注意:如果你想使用'关键事件',你应该设置表格的'KeyPreview'属性真实。


如果上面没有帮助,请提供更多详细信息。


问候,¥b $ b月光


Hi,

    I am having Windows form which contain tab control and in that i am having 4 tab i.e tabPage1,tabPage2,tabPage3,tabPage4. i want to move tabPage1 to tabPage2 up to tabPage4 while clicking on tab click from keyboard,i tried with tab control selectIndexChange event,keypress,keydown to move to another tabPage but its not working.

Please if any one have relevant information regarding this requirement ,share it.

Thanks Regards

Shankar

Hi Shankar,
You could try the following code(use left right to change the selected tabpage):

        private void tabControl1_KeyPress(object sender, KeyPressEventArgs e)
        {
            TabControl tb = sender as TabControl;
            if (tb.SelectedTab != null)
            {
                switch (e.KeyChar)
                {
                    case (char)Keys.Left:
                        if (tb.SelectedIndex > 0)
                            tb.SelectedIndex--;
                        break;
                    case (char)Keys.Right:
                        if (tb.SelectedIndex < tb.TabPages.Count - 1)
                            tb.SelectedIndex++;
                        break;
                    default:
                        break;
                }
            }
        }

Notice: If you want to use 'key events', you should set the form's 'KeyPreview' property to true.

If above is not help, please provide more detail info about this.

Regards,
Moonlight