tab page disable question
-
i'm tring to make an application that has a tab control with multiple tab pages. for integrity purposes, i want a tab page to be rendered disabled as soon as the user moves tot he next. the only problem is that there is no enabled property for tab pages and the canfocus and canselect properties are read only. Please help me!
rzvme
-
i'm tring to make an application that has a tab control with multiple tab pages. for integrity purposes, i want a tab page to be rendered disabled as soon as the user moves tot he next. the only problem is that there is no enabled property for tab pages and the canfocus and canselect properties are read only. Please help me!
rzvme
You are correct in that you cannot disable a tab page, but you can either prevent a user from going back to a previous tab page or you can allow a user to go back to a previous tab page but disable all the controls on that tab page. The sample code below will disable all controls on a previous tab.
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e) { if (tabControl1.SelectedIndex > highestTabIndex) { foreach (Control c in tabControl1.TabPages[highestTabIndex].Controls) { PropertyInfo pi = c.GetType().GetProperty("Enabled"); if (pi != null) pi.SetValue(c, false, null); } highestTabIndex = tabControl1.SelectedIndex; } }
The code is the event handler for the SelectedIndexChanged event of a tab control named tabControl1. It also assumes the existence of a form level variable declared as:
private int highestTabIndex = 0;
There are some limitations of the code, for instance if you start on tabPage1 and then click tabPage3 by skipping tabPage2 the code will disable only the controls on tabPage1. You would need to check the new index versus the old index to see if they only differ by one otherwise you could select the next page. So in this example when tabPage3 was selected by skipping tabPage2 you could change the selected tab to tabPage2 and then disable tabPage1. Tom Chester