[英]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 自定义控件进行一些更改:
this
reference to set its properties (see also this
keyword (C# Reference) ).this
引用来设置它的属性(另见this
关键字(C#参考) )。this.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
。 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.