I'm making a C# program that will generate a PictureBox:
private void Form1_Load(object sender, EventArgs e)
{
PictureBox picture = new PictureBox
{
Name = "pictureBox",
Size = new Size(16, 16),
Location = new Point(100, 100),
Image = Image.FromFile("hello.jpg"),
};
}
However, the control doesn't show up on my form. Why not?
you can try this.. you need use this.Controls.Add(picture);
private void Form1_Load(object sender, EventArgs e)
{
var picture = new PictureBox
{
Name = "pictureBox",
Size = new Size(16, 16),
Location = new Point(100, 100),
Image = Image.FromFile("hello.jpg"),
};
this.Controls.Add(picture);
}
and if you want to remove from form at runtime.
//remove from form
this.Controls.Remove(picture);
//release memory by disposing
picture.Dispose();
;
A control, such as a PictureBox, is just a class. There's nothing special about it, so a new PictureBox
won't magically appear on your form.
All you need to do after instantiating and initializing a control is add the control to the container's Controls collection:
this.Controls.Add(picture);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With