Kumar Saurav
Kumar Saurav

Reputation: 1

Enable or disable tab in Windows Forms

I am working on Windows Forms application. I have two tabs. On button click it should navigate to other and disable the first one. We should be able to look only the tab names. The click on the tab should not work on the disabled tab neither it should display anything. I have done the Add and Remove tabpage option but it is not giving the particular solution. How can I achieve to this?

Upvotes: 0

Views: 5502

Answers (3)

Fernando
Fernando

Reputation: 1

Actually, in .net Framework 4.5 the tabPage2.Enabled = false; doesn't work. May some control overcharge may do the trick.

Upvotes: 0

heq
heq

Reputation: 412

This works for me:

    private void Form1_Load(object sender, EventArgs e)
    {
        tabPage2.Enabled = false;
    }

    private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
    {
        if (!e.TabPage.Enabled)
            e.Cancel = true;
    }

Upvotes: 1

T. Fabre
T. Fabre

Reputation: 1527

You can use the SelectingEvent of the tab control. It is fired when the user clicks another tab, and it is cancellable, so you can prevent the user from changing tabs by checking the TabPage property or the TabPageIndex of the event args, and setting the Cancel property to true :

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        tabControl1.Selecting += new tabControlCancelEventHandler(tabControl1_Selecting);
    }

    private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
    {
        // This blocks the user from opening the second tab
        if (e.Action == TabControlAction.Selecting && e.TabPageIndex == 1)
            e.Cancel = true;
    }
}

Hope that helps

Upvotes: 0

Related Questions