adsfsd
adsfsd

Reputation: 11

how to make a control visible in a panel?

I want to add a control to my panel in WinForms.

public Form1()
{
       InitializeComponent();
       PictureBox a = new PictureBox();
       a.Left = 100;
       a.Top = 150;
       a.Width = 50;
       a.Height = 50; 
       a.BackColor = Color.Red;
       Controls.Add(a);    
}

Without the Panel, this code works perfect. But the Panel blocks the PictureBox, which properties should I change?

Upvotes: 0

Views: 131

Answers (1)

Michał
Michał

Reputation: 2282

Basically, there are few options to achieve that:

1. When you add the controls to your Form like this:

Controls.Add(panel);
Controls.Add(button1);
Controls.Add(button2);
Controls.Add(pictureBox);

They will be shown in this very order: panel on the bottom, buttons between and pictureBox on the top.

2. As it was pointed in the comments, you can use BringToFront() after adding the control.

pictureBox.BringToFront();

This will make the pictureBox to be on top of other things.

3. You can change the order of controls by editing their Z-index, called ChildIndex in WinForms. You can set it with:

Controls.SetChildIndex(pictureBox, __yourIndex__);

4. You can add the pictureBox to your panel with:

panel.Controls.Add(pictureBox);

Upvotes: 1

Related Questions