简体   繁体   English

如何以编程方式将自定义控件添加到表单并显示它?

[英]How to programmatically add a Custom Control to a Form and show it?

I'm trying to add a Label to a Windows Form by using another class programmatically.我正在尝试通过以编程方式使用另一个 class 将 Label 添加到 Windows 表单中。 My Label does not appear inside the Form.我的 Label 没有出现在表单中。
I don't know where I'm going wrong.我不知道我哪里错了。

private void Form1_Load(object sender, EventArgs e)
{
     Ticker ticker = new Ticker("ASDF");
     ticker.display();
}

public class Ticker : Label
{
     string labelText;
     Label label = new Label();

     public Ticker(string _labelText)
     {
         labelText = _labelText;
     }
     public void display()
     {
         label.Text = labelText;

         Controls.Add(label);
     }
}

You can make a few changes to your Ticker Custom Control:您可以对 Ticker 自定义控件进行一些更改:

  • You don't need to create a new Label inside your Custom Control: your Control is already a Label, use the this reference to set its properties (see also this keyword (C# Reference) ).你不需要你的自定义控件中创建一个新的 Label:你的控件已经是一个 Label,使用this引用来设置它的属性(另见this关键字(C#参考) )。
  • The Text is the current Label's Text ( this.Text ). Text 是当前标签的文本 ( this.Text )。 Store it if you need a copy of it for other reasons (custom painting, usually, so sometimes you need to clear the Text).如果您出于其他原因需要它的副本(通常是自定义绘画,因此有时您需要清除文本),请存储它。
    Controls is referring to the current class object: it's a Control, so it has a Controls property, which gets the ControlCollection of the child Controls of a Control. Controls指的是当前的class object:它是一个Control,所以它有一个Controls属性,它获取一个Control的子Controls的ControlCollection
  • You need to also specify a Point that defines the position of your Custom Control inside its Parent 's ClientRectangle .您还需要在其ParentClientRectangle中指定定义自定义控件的 position 的 Point 。
  • Even if it's not always required, add a parameter-less Constructor to your Custom Control: if/when it's actually needed, you'll have it already there.即使它并不总是需要,也可以在自定义控件中添加一个无参数构造函数:如果/当它实际需要时,你就已经在那里了。

If you don't want to set the Parent Control from the outside , as usual (eg, var label = new Label(); this.Controls.Add(label); ), you need to pass the reference of the Control which will become the Parent Control of your custom Label.如果您不想像往常一样从外部设置父控件(例如, var label = new Label(); this.Controls.Add(label); ),您需要传递控件的引用,这将成为您的自定义 Label 的父控件。
You can the use this reference - a Control type of reference - and add your Label to the Controls collection of the Control reference you receive:您可以使用此引用 - 一个Control类型的引用 - 并将您的 Label 添加到您收到的 Control 引用的 Controls 集合中:

// You want to store a reference to this Control if you need it later...
private Ticker ticker = null;

private void Form1_Load(object sender, EventArgs e)
{
    //... or just declare it with: var ticker = new Ticker() if you don't
    ticker = new Ticker("The Label's Text");
    // [this] of course refers the current class object, Form1
    ticker.Display(this, new Point(100, 100));
    // Or, display the Label inside a Panel, child of Form1
    // Note: if you don't comment the next line, the Label will be moved to panel1
    ticker.Display(this.panel1, new Point(10, 50));
}

Here, I'm overloading the Display() method, so it accepts both a Parent reference and a Point value, used to position the Control inside its Parent's Client Area.在这里,我重载了Display()方法,因此它同时接受 Parent 引用和Point值,用于 position 其父客户区域内的控件。
The Custom Label also calls BringToFront() on itself, to avoid showing up under some other, already existing, child Control of the new Parent .自定义 Label 也调用BringToFront() ,以避免出现Parent的其他一些已经存在的子控件下。

public class Ticker : Label
{
    public Ticker() : this("ticker") { }
    public Ticker(string labelText) => this.Text = labelText;

    public void Display(Control parent) => Display(parent, Point.Empty);
    public void Display(Control parent, Point position)
    {
        this.Location = position;
        parent.Controls.Add(this);
        this.BringToFront();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM