aurelio
aurelio

Reputation: 207

Working with multiple forms

I would like to load multiple forms within a form using user controls and I've tried the following code but nothing seems to happen after clicking on button1. Anyone knows what's wrong?

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        UserControl1 control = new UserControl1();
        control.Dock = DockStyle.Fill;
        this.Controls.Add(control);
    }
 }

Upvotes: 2

Views: 507

Answers (2)

Hans Passant
Hans Passant

Reputation: 942438

however the contents of UserControl1 seems to be overlapping and I can still see the content of Form1

The Z-order of the controls on the form matters. With Controls.Add(), the control ends up on the bottom of the order, existing controls overlap it. You fix it like this:

    this.Controls.Add(control);
    control.BringToFront();

Or use Controls.SetChildIndex() to insert it between controls.

Upvotes: 2

the_joric
the_joric

Reputation: 12261

Probably you need to change the value of Dock property. When it is DockStyle.Fill -- it will just take the whole area. Try to change it to other value, depending what layout you need.

Upvotes: 0

Related Questions