从类本身提升自定义控件的事件

时间:2013-10-02 10:45:20

标签: c# events

我有一个自定义TabPage类:

class CustomTabPage : TabPage
{
    TextBox tbCode = new TextBox();

    public CustomTabPage()
    {

    }

    public CustomTabPage(string title)
    {
        tbCode.Multiline = true;
        tbCode.Size = this.Size;  
    }        

    //Something like this...
    private void OnThisControlSizeChanged()
    {
        tbCode.Size = this.Size;
    }
}

当我从创建控件的位置调整大小时,我需要为此类提升OnSizeChanged控件的TabPage事件。这样做的原因是当我调整TabPage控件的大小时,我想相应地调整TextBox大小以使它们保持不变。

2 个答案:

答案 0 :(得分:1)

class CustomTabPage : TabPage
{
    TextBox tbCode = new TextBox();
    public CustomTabPage()
    {
        SizeChanged += CustomTabPage_SizeChanged;
    }

    void CustomTabPage_SizeChanged(object sender, EventArgs e)
    {
        OnThisControlSizeChanged();
    }

    public CustomTabPage(string title)
    {
        tbCode.Multiline = true;
        tbCode.Size = this.Size;
    }

    private void OnThisControlSizeChanged()
    {
        tbCode.Size = this.Size;
    }
}

答案 1 :(得分:0)

this.OnSizeChanged(new EventArgs());