6

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;
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
phapha pha
  • 319
  • 1
  • 5
  • 16

4 Answers4

12

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

Berkay Yaylacı
  • 4,383
  • 2
  • 20
  • 37
  • This looks "clean" when there is no content in the tabs. However, when I set e.Cancel = true, the NEXT tab get is "flashed" at me before it cancels back to the original tab. – gridtrak Jul 12 '21 at 21:09
2

Have a try on this

tabControl1.SelectTab(tabPage2);

On Index

tabControl1.SelectTab(1); 

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

Saad Suri
  • 1,352
  • 1
  • 14
  • 26
2

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);
    }
}
Community
  • 1
  • 1
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • Where should I put your code? Especially on => public class MyTabControl : TabControl. Should I create a new class named "MyTabControl" and inheriite it from TabControl? – phapha pha Nov 29 '16 at 17:03
  • 1
    Yest it's a new class. It's enough to copy the code and paste it in a new file in your project and after building the project the control will be added to ToolBox. Then you can drop an instance of the control on your form. – Reza Aghaei Nov 29 '16 at 17:06
  • 1
    You can see the difference between the first one `MyTabControl` and the second one `WizardControl` at run-time. At design time, both of them are like standard `TabControl`, but at run-time, `WizardControl` doesn't show tab headers. It will act like a multi-panel which let you at design time work with all tab pages but at run-time you can just switch between tabs using code, like what is expected from a wizard. `MyTabControl` at rum-time is like a normal `TabControl` with header but to switch between tabs you should use code (for example click event of a button to set selected tab). – Reza Aghaei Nov 29 '16 at 17:10
-1

One of the options is to try simple IsHitTestVisible="False"

SHR
  • 7,940
  • 9
  • 38
  • 57