Disable switching between tabs by click or keys in TabControl

我的梦境 提交于 2019-11-26 23:39:18

问题


So guys, is it possible to switch to another tab by ONLY using NEXT button?

This is mean that you CAN'T switch to another tab page by clicking that other tab.

The code that I usually use on the NEXT button are something like this :

tabControl1.SelectedTab = tabPage2;

回答1:


You can set ControlStyles.UserMouse to true. This way you can simply disable mouse on tab headers.

By the way, just disabling click on headers is not enough and you need to disable keys which let the user to switch to between tabs, like Shift+Tab, Ctrl+Shift+Tab, , , Home and End.

using System.Linq;
using System.Windows.Forms;
using System.ComponentModel;
public class MyTabControl : TabControl
{
    public MyTabControl()
    {
        if (LicenseManager.UsageMode != LicenseUsageMode.Designtime)
            SetStyle(ControlStyles.UserMouse, true);
    }
    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        var filteredKeys = new Keys[]{(Keys.Control | Keys.Tab),
            (Keys.Control | Keys.Shift | Keys.Tab),
            Keys.Left, Keys.Right, Keys.Home, Keys.End};
        if (filteredKeys.Contains(keyData))
            return true;
        return base.ProcessCmdKey(ref msg, keyData);
    }
}

Note: If you like to have a wizard-like control (tab control without header), you can handle TCM_ADJUSTRECT like this. You should disable those keys also in that solution too. Here is a changed version:

using System.Linq;
using System.Windows.Forms;
using System.ComponentModel;
public class WizardControl: TabControl
{
    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        var filteredKeys = new Keys[]{(Keys.Control | Keys.Tab),
            (Keys.Control | Keys.Shift | Keys.Tab),
            Keys.Left, Keys.Right, Keys.Home, Keys.End};
        if (filteredKeys.Contains(keyData))
            return true;
        return base.ProcessCmdKey(ref msg, keyData);
    }
    public const int TCM_FIRST = 0x1300;
    public const int TCM_ADJUSTRECT = (TCM_FIRST + 40);
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == TCM_ADJUSTRECT && !DesignMode)
           m.Result = (IntPtr)1;
        else 
           base.WndProc(ref m);
    }
}



回答2:


TabControls Selecting Event will disable switching, but we need to keep track of button's click with a bool value, otherwise button's click won't select the tab either.

bool checkCancel = true;
private void button2_Click(object sender, EventArgs e)
{
    checkCancel = false;
    tabControl1.SelectTab("tabPage2");
}

private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
{
    e.Cancel = checkCancel;
    checkCancel = true;
}

Result, (btw trying to click to tabpages at the gif :))

Hope helps,




回答3:


Have a try on this

tabControl1.SelectTab(tabPage2);

On Index

tabControl1.SelectTab(1); 

Tablist is 0 based index so "1" is the second tab.



来源:https://stackoverflow.com/questions/40630281/disable-switching-between-tabs-by-click-or-keys-in-tabcontrol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!