CodeGeek
CodeGeek

Reputation: 33

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. My Label does not appear inside the Form.
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);
     }
}

Upvotes: 1

Views: 1614

Answers (1)

Jimi
Jimi

Reputation: 32248

You can make a few changes to your Ticker Custom Control:

  • 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)).
  • The Text is the current Label's 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.
  • You need to also specify a Point that defines the position of your Custom Control inside its Parent's ClientRectangle.
  • 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 (e.g., 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.
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:

// 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.
The Custom Label also calls BringToFront() on itself, to avoid showing up under some other, already existing, child Control of the new 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();
    }
}

Upvotes: 2

Related Questions