Search code examples
c#winformstabcontroltabpage

C# Change the Default TabPage in TabControl using Custom TabPage


I created a Custom TabControl and a custom TabPage like below:

Custom TabControl code:

public class MyCustomTabControl : TabControl
{
   //Some Custom Properties

   public MyCustomTabControl () : base()
    {
        base.Width = 200;
        base.Height = 100;

    }
}

CustomTabPage:

public class MyCustomTabPage : TabPage
{
    //Some Custom Properties

    public MyCustomTabPage() : base()
    {                     
        this.BackColor = System.Drawing.Color.Transparent;
    }
}

How can I do it so that when I add my custom control MyCustomTabControl in the form, it add the custom TabPage named MyCustomTabPage. Currently it adding the TabPage from windows.


Solution

  • You Need to do some steps, first define a class e.g MyCustomTabCollection and implement all Three Interfaces methods for your MyCustomTabCollection class, then Declare an instance of MyCustomTabCollection on your MyCustomTabControl as a public property.

    Implement interfaces

    public class MyCustomTabPageCollection : IList, ICollection, IEnumerable
    {
        // implement all three interfaces
    }
    

    Implement all methods

    public object this[int index] { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
    
    public bool IsReadOnly => throw new NotImplementedException();
    
    public bool IsFixedSize => throw new NotImplementedException();
    
    public int Count => throw new NotImplementedException();
    
    public object SyncRoot => throw new NotImplementedException();
    
    public bool IsSynchronized => throw new NotImplementedException();
    
    public int Add(object value)
    {
        throw new NotImplementedException();
    }
    
    public void Clear()
    {
        throw new NotImplementedException();
    }
    
    public bool Contains(object value)
    {
        throw new NotImplementedException();
    }
    
    public void CopyTo(Array array, int index)
    {
        throw new NotImplementedException();
    }
    
    public IEnumerator GetEnumerator()
    {
        throw new NotImplementedException();
    }
    
    public int IndexOf(object value)
    {
        throw new NotImplementedException();
    }
    
    public void Insert(int index, object value)
    {
        throw new NotImplementedException();
    }
    
    public void Remove(object value)
    {
        throw new NotImplementedException();
    }
    
    public void RemoveAt(int index)
    {
        throw new NotImplementedException();
    }
    

    Declare your CustomTabPageCollection

    public class MyCustomTab : TabControl
    {
        public MyCustomTabPageCollection TabPages { get; set; }
    
        public MyCustomTab() : base()
        {
            base.Width = 200;
            base.Height = 100;
    
        }
    }
    

    If there is a problem yet, let me know.