Reputation: 2116
I'm having trouble displaying a PictureBox in C#. I have two forms. In my main form I'm calling the other form, where the PictureBox is located.
This is how I am calling the second form:
frmODeck oDeck = new frmODeck();
oDeck.Show();
Now, this is my second form, where the PictureBox is located from main form
namespace Shuffle_Cards
{
public partial class frmODeck : Form
{
private PictureBox picBox;
private Image image;
public frmODeck()
{
InitializeComponent();
}
private void frmODeck_Load(object sender, EventArgs e)
{
image = Image.FromFile("C:\\C2.jpg");
picBox = new PictureBox();
picBox.Location = new Point(75, 20);
picBox.Image = image;
picBox.Show();
}
public void getCards()
{
}
}
}
What am I doing wrong, or what am I missing?
Thanks
Upvotes: 4
Views: 3230
Reputation: 113402
The picture-box control needs to be added to the control-collection of the top-level control it should belong to - in the case, the form itself. Relevant: Control.Controls
.
Replace:
picBox.Show();
with:
Controls.Add(picBox);
Upvotes: 5
Reputation: 4637
Befor you do a picBox.Show(); , you need to add it to the Controls of the window you are loading, with the code @Ani provided:
Controls.Add(picBox);
That should do it!
Upvotes: -1