1

I don't understand why this is not displaying a new PictureBox to the form:

private void Form1_Load(object sender, EventArgs e)
        {
            generateTable();
        }
        public void generateTable()
        {
            //setting up the background tiles
            PictureBox setup = new PictureBox();
            setup.Width = 100;
            setup.Height = 100;
            setup.Location  = new Point(100,100);
            setup.Image = new Bitmap(Application.StartupPath+@"\BlankArea.png");
            setup.Visible = true;
            this.Controls.Add(setup);
        }

It does find the image (tested with another picturebox).

user2977903
  • 25
  • 2
  • 7
  • Does the code step through the debugger okay? – Chris Walsh Aug 15 '14 at 13:03
  • Yes, the code runs perfectly fine but the picturebox does not show up on the form. – user2977903 Aug 15 '14 at 13:06
  • Pretty unclear why you are not just adding the PB with the designer. Having it covered by another control at the same location is the typical mishap. And [this mishap](http://stackoverflow.com/a/4934010/17034) is common enough. – Hans Passant Aug 15 '14 at 14:41
  • 1
    Try: `setup.BringToFront();`, setting a Border and looking the Control's properties in the debugger! – TaW Aug 15 '14 at 17:45

3 Answers3

0

if you runing applocation thorught VisualStudio your image BlankArea.png shold bee in bin\Debug folder of your exe project.

Your code works for me.

0

Are you sure that the Form1_Load event callback is tied into the Form1 via the Designer?

Also, have you checked to see that your image is OK? Try setting the Background color of "setup" to something that will stand out -- like Red.

Locke
  • 1,133
  • 11
  • 32
0

As suggested by @TaW the localical answer is that the Picturebox is being created and added behind the form itself. Using the following code worked perfect for me!

//Apply the correct icon
if (icon != MessageBoxIcon.None)
{
    PictureBox pbIcon = new PictureBox();

    pbIcon.SizeMode = PictureBoxSizeMode.AutoSize;
    switch (icon)
    {
        case MessageBoxIcon.Asterisk:
            pbIcon.Image = SystemIcons.Asterisk.ToBitmap();
            break;
        case MessageBoxIcon.Question:
            pbIcon.Image = SystemIcons.Question.ToBitmap();
            break;
    }
    pbIcon.Location = new Point(0, 0);

    this.Controls.Add(pbIcon);
    pbIcon.BringToFront();
}

In this context, icon is...

MessageBoxIcon icon = MessageBoxIcon.Question;
Arvo Bowen
  • 4,524
  • 6
  • 51
  • 109