Click here to Skip to main content
16,005,389 members
Home / Discussions / C#
   

C#

 
GeneralRe: TabControl Pin
zaboboa26-Jul-05 7:22
zaboboa26-Jul-05 7:22 
GeneralRe: TabControl Pin
Alomgir Miah26-Jul-05 7:31
Alomgir Miah26-Jul-05 7:31 
GeneralRe: TabControl Pin
zaboboa26-Jul-05 7:37
zaboboa26-Jul-05 7:37 
GeneralRe: TabControl Pin
Alomgir Miah26-Jul-05 8:07
Alomgir Miah26-Jul-05 8:07 
GeneralRe: TabControl Pin
Luis Alonso Ramos26-Jul-05 8:34
Luis Alonso Ramos26-Jul-05 8:34 
GeneralRe: TabControl Pin
zaboboa27-Jul-05 3:20
zaboboa27-Jul-05 3:20 
GeneralRe: TabControl Pin
Luis Alonso Ramos27-Jul-05 4:37
Luis Alonso Ramos27-Jul-05 4:37 
GeneralRe: TabControl Pin
Luis Alonso Ramos26-Jul-05 13:50
Luis Alonso Ramos26-Jul-05 13:50 
I already needed something similar, so I set up to implement it. Here's the result.
/// <summary>
/// Delegate that defines a handler for the SelectedIndexChanging event.
/// </summary>
public delegate void TabControlCancelEventHandler(object sender,
    TabControlCancelEventArgs e);
 
/// <summary>
/// Derived tab control that adds SelectedIndexChanging event.
/// </summary>
public class TabControlEx : TabControl
{
    /// <summary>
    /// Fired when the selected tab is about to change, allowing the
    /// receiver of the event to cancel.
    /// </summary>
    public event TabControlCancelEventHandler SelectedIndexChanging;
 
    /// <summary>
    /// Default constructor. Initializes a new TabControlEx object.
    /// </summary>
    public TabControlEx() 
    {
    }
 
    /// <summary>
    /// Called when index has changed. Raises SelectedIndexChanged event.
    /// </summary>
    /// <param name="e">Event arguments.</param>
    protected override void OnSelectedIndexChanged(EventArgs e)
    {
        // Disallow nesting of this event
        if(newTab != -1)
            return;
 
        try
        {
            // Save new tab, and return tu current tab
            newTab = SelectedIndex;
            SelectedIndex = selectedTab;
 
            // Raise SelectedIndexChanging event
            TabControlCancelEventArgs e2 = new TabControlCancelEventArgs(newTab);
            OnSelectedIndexChanging(e2);
 
            // If user chose to cancel, stay in current tab
            if(e2.Cancel)
                return;
 
            // Now we can select the requested tab
            SelectedIndex = newTab;
            selectedTab = SelectedIndex;
 
            // Finally call registered event handlers
            base.OnSelectedIndexChanged(e);
        }
        finally
        {
            newTab = -1;
        }
    }
 
    /// <summary>
    /// Raises SelectedIndexChanging event.
    /// </summary>
    /// <param name="e">Event arguments.</param>
    protected virtual void OnSelectedIndexChanging(TabControlCancelEventArgs e)
    {
        // If any handler is registered, raise the event
        if(SelectedIndexChanging != null)
            SelectedIndexChanging(this, e);
    }
 
    private int selectedTab = 0;
    private int newTab      = -1;
}
 
/// <summary>
/// Contain arguments for TabControlEx's SelectedIndexChanging event.
/// </summary>
public class TabControlCancelEventArgs : CancelEventArgs
{
    /// <summary>
    /// Constructor. Initializes a new TabControlCancelEventArgs.
    /// </summary>
    /// <param name="tab">Index of tab page that will be selected.</param>
    public TabControlCancelEventArgs(int tab)
        : base()
    {
        this.tab = tab;
    }
 
    /// <summary>
    /// Constructor. Initializes a new TabControlCancelEventArgs.
    /// </summary>
    /// <param name="tab">Index of tab page that will be selected.</param>
    /// <param name="cancel">true to cancel the selection change,
    /// false to allow it.</param>
    public TabControlCancelEventArgs(int tab, bool cancel)
        : base(cancel)
    {
        this.tab = tab;
    }
 
    /// <summary>
    /// Gets the index of the tab page that will be selected.
    /// </summary>
    public int Tab
    {
        get { return tab; }
    }
 
    private int tab;
}
To use it, just subscribe to the SelectedIndexChanging event, and set e.Cancel to false if necessary.

Good luck!

-- LuisR

P.S. Sorry for the XML comments. Sniff | :^)



Luis Alonso Ramos
Intelectix - Chihuahua, Mexico

Not much here: My CP Blog!

GeneralRe: TabControl Pin
zaboboa27-Jul-05 6:51
zaboboa27-Jul-05 6:51 
GeneralRe: TabControl Pin
Luis Alonso Ramos27-Jul-05 7:01
Luis Alonso Ramos27-Jul-05 7:01 
GeneralRe: TabControl Pin
zaboboa27-Jul-05 7:34
zaboboa27-Jul-05 7:34 
GeneralRe: TabControl Pin
Luis Alonso Ramos27-Jul-05 7:40
Luis Alonso Ramos27-Jul-05 7:40 
GeneralRe: TabControl Pin
zaboboa27-Jul-05 7:53
zaboboa27-Jul-05 7:53 
GeneralRe: TabControl Pin
Luis Alonso Ramos27-Jul-05 7:58
Luis Alonso Ramos27-Jul-05 7:58 
GeneralCustom cursor as resource Pin
Will L Pittenger26-Jul-05 6:26
Will L Pittenger26-Jul-05 6:26 
GeneralRe: Custom cursor as resource Pin
[Marc]26-Jul-05 8:37
[Marc]26-Jul-05 8:37 
GeneralRe: Custom cursor as resource Pin
Will L Pittenger26-Jul-05 13:18
Will L Pittenger26-Jul-05 13:18 
GeneralRe: Custom cursor as resource Pin
[Marc]26-Jul-05 14:11
[Marc]26-Jul-05 14:11 
GeneralRe: Custom cursor as resource Pin
Will L Pittenger27-Jul-05 7:18
Will L Pittenger27-Jul-05 7:18 
GeneralRe: Custom cursor as resource Pin
[Marc]27-Jul-05 8:21
[Marc]27-Jul-05 8:21 
GeneralRe: Custom cursor as resource Pin
Will L Pittenger27-Jul-05 10:02
Will L Pittenger27-Jul-05 10:02 
GeneralRe: Custom cursor as resource Pin
[Marc]27-Jul-05 10:50
[Marc]27-Jul-05 10:50 
GeneralRe: Custom cursor as resource Pin
Will L Pittenger27-Jul-05 17:44
Will L Pittenger27-Jul-05 17:44 
GeneralRe: Custom cursor as resource Pin
[Marc]27-Jul-05 20:12
[Marc]27-Jul-05 20:12 
GeneralMemory Leak Pin
dbetting26-Jul-05 6:06
dbetting26-Jul-05 6:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.