如何在C#中将控件添加到另一个控件?

时间:2012-07-08 10:33:08

标签: c# user-controls

我想通过扩展现有控件来创建自定义控件。实际上,我想在原始控件中添加一些功能。如何在其构造函数或其他任何位置对我的自定义控件进行另一个控件(如TextBox之类的控件)?

public partial class AdvancedKnob : KnobControl
{

    private DoubleInput Field_ValueControl =  null;

    public AdvancedKnob()
    {
        this.InitializeComponent();
        this.Field_ValueControl = new DoubleInput();
        this.Container.Add(this.Field_ValueControl); //DOES NOT WORK!!
    }
}

2 个答案:

答案 0 :(得分:2)

试试这个:

this.Controls.Add(this.Field_ValueControl);

有关详细信息,请转到:How to programmatically add controls to Windows forms at run time by using Visual C#

答案 1 :(得分:0)

使用

this.Controls.Add(control);