Home > Blockchain >  How to programitically add a panel winforms c#
How to programitically add a panel winforms c#

Time:10-30

i know this is kinda a silly question but how do i add panel to my winforms project at runtime i wanted a panel to show at startup but instead, i get nothing (No error messages were found) here is the code:

private void Form1_Load(object sender, EventArgs e)
{
 Panel panel = new Panel();
 panel.Size = new Size(200, 100);
 panel.Location = new Point(20,20);
 this.Controls.Add(panel);
 panel.Show();
}

ive tried using panel.Visibility = true; but its not working :(

CodePudding user response:

Here is a sample for it. A panel alone is a container and not a visible component, you should have something in it. ie:

void Main()
{
    Form f = new Form();
    f.Show();
    MessageBox.Show("Will add panel");
    Panel p = new Panel { Size = new Size(200, 100), Location = new Point(20, 20) };
    f.Controls.Add(p); // nothing would show
    MessageBox.Show("Panel added. Continue to add something in panel");

    Label l = new Label { Left=10, Top=10, Text="A label inside the panel" };
    p.Controls.Add(l);
}
  • Related