I haven't found a solution yet related to this problem. I just to disable other tabpages in my Winforms TabControl when a certain tabpage is open. So not hiding them but disable the function to open them when one clicks a tab page. It just should be displayed grey. Is this possible? I've read something about a "Selected" event but don't know how to use that.
Asked
Active
Viewed 1,540 times
1
-
How will you re-enable the other pages? – TaW Mar 12 '15 at 19:16
-
http://stackoverflow.com/questions/418006/how-can-i-disable-a-tab-inside-a-tabcontrol – Sybren Mar 12 '15 at 19:18
-
This link didn't help me. I don't know how to use the code the right way – Mar 12 '15 at 19:21
1 Answers
3
You can use the Selecting
event:
Create a class level variable:
int lockedPage = -1;
If it is set to the index of a TabPage
you can select it but you can't leave it, i.e. you can't select any other page.
private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
{
if (lockedPage >= 0 && e.TabPageIndex != lockedPage) e.Cancel = true;
}
If you set lockedPage = 0;
you prevent the user from leaving the 1st page etc..
To re-enable the selection of other pages set it to -1

TaW
- 53,122
- 8
- 69
- 111
-
How to use that when I want that every other tab is disabled when one certain is enabled? – Mar 12 '15 at 19:22
-
-
I have expanded the code a little. You need to include some code to set the flag variable to be less than zero.. – TaW Mar 12 '15 at 19:32
-
-
-
-
It's tabControl2. So I changed it to tabControl2. I'm calling it from a method of another form. I've placed it in my form1 (That works) but when I call it with form1.tabControl2_Selecting he doesn't recognize it – Mar 12 '15 at 19:39
-
To access it from another form you need a public reference to it as usual.. Many, many posts explain how to do that..! – TaW Mar 12 '15 at 19:40
-
Yes I made it public and try to call it with form1.tabControl2_Selecting(this, eventArgs.Empty) like usual but this time it doesn't work. He can't find the arguments. – Mar 12 '15 at 19:44
-
Um. No! The selected event should stay with the Tab control!! If you need to control the lock from another form you need to access the `lockedPage`variable. And since it is in the form1 you need 1) to make it public, best a public property: `int lockedPage {get; set;}` 2) a reference to form1 in form2. One common way it to pass the form in the constructor..[Look here](http://stackoverflow.com/questions/28968575/is-it-possible-to-create-a-dialog-which-only-blocks-its-parent/28969335#28969335) for an example of this technique! If you make `lockedPage` a property, don't forget to intialize it! – TaW Mar 12 '15 at 19:54
-
Ok I tried that now but I need to call the method somehow from my second form. Otherwise I can't call the event. I even tried it with (null,null) and I get a nullreference exception – Mar 12 '15 at 20:06
-
No, you do not call events. __Never__. (Almost). __Not here__!! (You __couldn't__ create the __necessary__ parameters!) The event happens by itself, that is by the user clicking at some tab.. Make sur it is hooked up in the events tab of the property window! – TaW Mar 12 '15 at 20:09
-
-
Not a problem, but a __very basic__ thing. Try Google for any amount of nice tutorials: "C# hooking up events". Or explore the parperty tab for the event tab (with the arrow) by yourself.. – TaW Mar 12 '15 at 20:15
-
Ok I found it! It works! Thank you very much. That was the thing I was looking for! – Mar 12 '15 at 20:16