Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable switching between tabs by click or keys in TabControl

enter image description here

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;
like image 696
phapha pha Avatar asked Nov 16 '16 10:11

phapha pha


2 Answers

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 :))

enter image description here

like image 98
Berkay Yaylacı Avatar answered Nov 14 '22 21:11

Berkay Yaylacı


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);
    }
}
like image 27
Reza Aghaei Avatar answered Nov 14 '22 23:11

Reza Aghaei