How can I cancel the selecting Event for a TabControl

Go To StackoverFlow.com

0

I am handling tab change in the WinForms TabControl's Deselecting event. However, in some cases I am deleting the tab that I clicked on before I switch to it.

scenario

I have tab 1 and tab 2 currently I'm in tab 1 I click on tab 2 tab 1 Deselecting Event Removes Tab 2 from the tab collection Crashes at OnPaint because it's trying to go to a tab that no longer exists. (ArgumentOutOfRangeException). It crashes before it hits the Selecting Event.

I don't want to see if the tabcount changed in deselecting because I only want to cancel if the tab I'm going to no longer exists.

Any help would be greatly appreciated.

    private void TabControl_Deselecting( object sender, TabControlCancelEventArgs ) {
       DoSomeWork();
    }

Assume that DoSomeWork deletes the Tab I clicked on. How can I find out if it did delete the tab I was intending to go to?

2012-04-03 20:22
by fbhdev
Could you clarify the question a little more and what the desired outcome is @fahed. Having trouble understanding what you want here - ImGreg 2012-04-03 20:50
You need to rethink your user interface. It sounds way too confusing and difficult to use - BoltBait 2012-04-03 21:03
the Tab removal is 3 or 4 functions deep, whoa thats a bit over the top. Are you binding controls to business objects and putting ALL the logic in the business object? The way this GUI functions sounds really un-intuitive - Jeremy Thompson 2012-04-04 03:20


1

No repro. The scenario is strange but I can't get it to crash. Do make sure you cancel the Deselect.

    private void tabControl1_Deselecting(object sender, TabControlCancelEventArgs e) {
        if (e.TabPageIndex == 0 && tabControl1.TabCount > 1) {
            tabControl1.TabPages[1].Dispose();
            e.Cancel = true;
        }
    }
2012-04-03 20:50
by Hans Passant
let me see how i can get e.cancel set to true in deselect. that would solve the issue - fbhdev 2012-04-03 21:07
the problem is that the tab removal is 3 or 4 functions deep in the cod - fbhdev 2012-04-03 21:08
Whatever you are doing, it is completely invisible from here, you are doing it wrong. You'll need to document your question better, a snippet that reproduces the problem is required - Hans Passant 2012-04-03 21:19


0

One of possible solution that comes in my mind:

if you are sure that Tab1 Deselect event raised before Tab2 Select event, I would try, to declare my custom tab control and override it's OnPaintMethod, like this pseudocode:

public class MyCustomTab : TabItem
{
   ...


   protected override OnPaint(....)
   {
      if(this.Parent == null) return;

       base.Paint(...);
   }    
}

Clear that to your TabControl you should add TabItems of that type.

2012-04-03 20:29
by Tigran
Ads