Click here to Skip to main content
15,898,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
how can i disable and enable a tabpage of tabcontrol?
for ex., i have 2 buttons by name as Start and Stop and 2 tabpages. when i press on Start, tabpage no.1 is disabled and just can access to tabpage no.2!(it means i can see 2 tabpages by their names, but can't access to tabpage no.1! and means not to remove or hide tabpage no.1!!). when i press on Stop, tabpage no.2 is disabled and just can access to tabpage no.1!
how can i disable(not hide and remove) tabpage as a textbox, when we disable it?
Posted

If you are talking about WinForms, the only way to do it is to loop through all the controls on that tab page and disable them one by one. For example:

C#
private void DisableTabPage(TabPage tbPg)
{
    foreach (Control c in tbPg.Controls)
        c.Enabled = false;
}

private void EnableTabPage(TabPage tbPg)
{
    foreach (Control c in tbPg.Controls)
        c.Enabled = true;
}


You would then call it in code with:

C#
//To disable
DisableTabPage(tabPage1);

//Or to enable
EnableTabPage(tabPage1);


TabPage does have an .Enabled property but it doesn't do anything (I don't think it shows up in Intellisense, it also has a .Visible property that doesn't show up or do anything). Shame that in WinForms they don't have a way to do it.

However, if you are talking about WPF, then you can disable them using the .IsEnabled or .IsVisible property, but since you used the word TabPage instead of TabItem I'm guessing you are using WinForms.
 
Share this answer
 
If you are talking about WinForms tab control, you cannot disble tab page.

You can try to simulate behavior you need by intercepting SelectedIndexChanged event of tab control.
If selected tab page is "disabled" one, you can reassign SeletedTab to the "enabled" one.


Something like this:

C#
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (tabControl1.SelectedTab == tabPage3)
        tabControl1.SelectedTab = tabPage1;
}
 
Share this answer
 
Comments
mohsenvasefi 26-Nov-13 9:18am    
good, but it can't help me :-( i need to disable some tabpages!
Adam Zgagacz 26-Nov-13 9:44am    
It depends what you define as "disbaled". If "disabled" means you cannot select it. My code does the job.
Tab page can be disabled by using the ID of the Tab you want to disabled -

tabItinerary.Enabled=false
 
Share this answer
 
Comments
mohsenvasefi 26-Nov-13 6:07am    
what is it? if you want to disable tabpage1, how do you write it?

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