Tomato Ketchup
Tomato Ketchup

Reputation: 27

Location of button in a panel not in the form

I want to place a new button just below the button which initialized it. But this one is in a panel and not in the Form.

The panel is located at (45,213) in the form and the button is at (31 - 40) in the panel. The panel will move in the future; that's why I would like to take the panel as a reference, not the form.

My code is

private void addstrat3_i_Click(object sender, EventArgs e)
{
    panel3strat.Width += 200;
    Button addstrat3_2 = new Button();
    this.Controls.Add(addstrat3_2); 
    addstrat3_2.Size = new Size(210, 41);
    addstrat3_2.Location = new Point(31,100);
    addstrat3_2.Visible = true;
    this.Controls.Add(addstrat3_2); 
 }

Upvotes: 1

Views: 78

Answers (1)

KingCronus
KingCronus

Reputation: 4519

Every containing type has it's own Controls property.

Instead of:

this.Controls.Add()

You could use:

myPanel.Controls.Add()

That way the control you add is associated the the correct parent object.

Upvotes: 4

Related Questions