Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Developers,

I have tabcontrol (tab_control) with 3 tab pages (tb1,tb2,tb3)in the Windows Form. When I load the form I want to disable to access the tb3 pages. User can see that page, but user couldn't be able to access the tb3 page. Like textbox.Enable=false;

I try that below code but it is not working

C#
 Form Load
{
 tab_control.SelectedIndex[2].Enable = false;
}


Regards
Vasanthakumar
Posted
Updated 11-Sep-12 7:54am
v2

Add a Selecting event to your TabControl:

C#
private void tab_control_Selecting(object sender, TabControlCancelEventArgs e)
{
    if (e.TabPage == tb3)
    {
        e.Cancel = true;
    }
}

If the user selects tb3, this code cancels the selecting.
 
Share this answer
 
v2
Comments
Member 10876869 23-Mar-17 12:44pm    
Thanks
Hi,
As far as I know, a tab page doesn't have an Enabled property. In the code you use, SelectedIndex only set or get the tab controls selected index, it does not change the property of the page.
You can achieve similar effect by disabling the controls in that page or cancel the selection event.
This is one of the easy way of disabling the contents in the page

C#
((Control)this.tb3).Enabled = false;


Or you can cancel the selection event if user select tb3.

C#
private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
{
    if (this.tabControl1.SelectedTab.Name == "tb3")
    {
        e.Cancel = true;
    }
}


This way, user cannot even view the content of the page, they just know there's a tb3 exists. Note that the event handeler is tabControl1_Selecting not tabControl1_SelectedIndexChanged. In your case I think the second option fits your requirement more.
Hope this is helpful.
 
Share this answer
 
If you are interested you can hide them by creating your own TabControl:

C#
public class MyTabControl : TabControl
{
    protected override void WndProc(ref Message m)
    {
        // Hide tabs by trapping the TCM_ADJUSTRECT message
        if (m.Msg == 0x1328 && !DesignMode)
           m.Result = IntPtr.Zero;
        else
           base.WndProc(ref m);
    }
}


You can navigate between TabPages by SelectedIndex++ or SelectedIndex--
 
Share this answer
 
v2
C#
//Move&Add is not good answer

this.tabPage1.Parent = null; // hide

this.tabPage1.Parent = this.tabControl1; //show
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900