Francis.Beauchamp
Francis.Beauchamp

Reputation: 1363

TabControl AddingTab event

I have a TabControl in which I want to prevent adding existing TabPage (they are identified by a name) and instead set the SelectedTabPage to this precise tab.

I wish to know if there are an event that triggers right before a page is being added to the TabControl. If not, would using the event CollectionChanged of the TabPages (list) be a correct alternative ?

Upvotes: 1

Views: 2809

Answers (2)

Mark Hall
Mark Hall

Reputation: 54532

Try something like this, I am checking the TabControl page Collection for a page with the same name as the Page that is trying to be added, if it exists I am setting focus to the existing instance, otherwise adding the new page to the TabControl. See if something like this works for you.

private void button1_Click(object sender, EventArgs e)
{
    TabPage tp = new TabPage();
    tp.Name = tabPage1.Name;
    var temp =tabControl1.Controls.Find(tp.Name,true);
    if( temp.Length > 0)
    {
        tabControl1.SelectedTab = (TabPage) temp[0];
    }
    else
        tabControl1.Controls.Add(tp);
}

Anything having to do with the ControlCollection will most likely be triggered after the control has been added.

From above link:

You can determine if a Control is a member of the collection by passing the control into the Contains method. To get the index value of the location of a Control in the collection, pass the control into the IndexOf method. The collection can be copied into an array by calling the CopyTo method.


If you want you could cleanup your code some by adding an ExtensionMethod to your TabControl Check for an existing page, set focus or add from there.

Example:

namespace ExtensionMethods
{
    public static class MyExtensions
    {
        public static bool AddPage(this TabControl tc, TabPage tp)
        {
            var matchedPages = tc.Controls.Find(tp.Name, false);
            if ( matchedPages.Length > 0)
            {
                tc.SelectedTab = (TabPage)matchedPages[0];
                return true;
            }
            else
            {
                tc.TabPages.Add(tp);
                tc.SelectedTab = tp;
                return false;
            }

        }
    }
}

Usage:

tabControl1.AddPage(tp);

Upvotes: 1

Forest Kunecke
Forest Kunecke

Reputation: 2160

I believe the event you're looking for is the Control.ControlAdded event:

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.controladded.aspx

If that also detects when things inside the tab pages themselves are added, you should be able to filter out everything but TabPage controls using the ControlEventArgs.Control property in your event handler.

To reject adding a control will be a little more complicated. Since this event seems to only be raised after the control gets added, you'll need to do something like this:

void onControlAdded(object sender, ControlEventArgs e) {
    var tab = e as TabPage;

    if (tab == null)
        return;

    this.myTabControlObject.TabPages.Remove(tab);
}

This should remove the tab, but it will likely slow the tab adding process considerably.

Upvotes: 1

Related Questions