C# 使用自定义 TabPage 更改 TabControl 中的默认 TabPage

C# Change the Default TabPage in TabControl using Custom TabPage

我创建了一个自定义 TabControl 和一个自定义 TabPage,如下所示:

自定义TabControl代码:

public class MyCustomTabControl : TabControl
{
   //Some Custom Properties

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

    }
}

自定义TabPage:

public class MyCustomTabPage : TabPage
{
    //Some Custom Properties

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

我该怎么做才能在表单中添加自定义控件 MyCustomTabControl 时,它会添加名为 MyCustomTabPage 的自定义 TabPage。目前它从 windows.

添加 TabPage

您需要执行一些步骤,首先定义一个 class 例如 MyCustomTabCollection 并为您的 MyCustomTabCollection class 实现所有三个接口方法,然后声明一个实例MyCustomTabCollection 在你的 MyCustomTabControl 作为 public 属性.

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;

    }
}

如果还有问题,请告诉我。