Search code examples
c#winformsimagepicturebox

C# PictureBox - Can't make it work


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


Solution

  • 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);