1

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.

1 Answers1

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
  • Loop through all tabpages when one is disabled and disable them – Sybren Mar 12 '15 at 19:23
  • 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
  • @Jackson30 Your "one" active tab is *acting* more like a dialog box. – LarsTech Mar 12 '15 at 19:33
  • He can't find tabControl1_Selecting –  Mar 12 '15 at 19:34
  • What is the name of your `Tab` control? Not `tabControl1` ? – TaW Mar 12 '15 at 19:35
  • 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
  • Ok that will be the problem. How to do that? –  Mar 12 '15 at 20:13
  • 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