• 0

[C#] Issue with making one picturebox appear ontop of another


Question

Hey,

I am currently working on a program to teach myself C#. In it, a person is "throwing" meatloaf at a picture of Hilary Clinton's head. (long story). I've created the picturebox containing Hilary's head from the toolbox, but I created my meatloaf pictureboxes from code. I have right clicked on Hilary's picture and selected "Send to Back", and used the Meatloaf.BringToFront();, but for some reason, the meatloaf always goes behind Hilary's head, making it hidden.

The code I used to set up the Meatloaf pictureboxes is

(global)
public PictureBox[] picNewMeatLoaf = new PictureBox[20];

(In a FormLoad)

			for (int j = 1; j <= 19; j++)
			{
				picNewMeatLoaf[j] = new PictureBox();
				picNewMeatLoaf[j].BringToFront();
			}
__________________________________
(In a AddMeatLoaf() procedure)

this.SuspendLayout();

   picNewMeatLoaf[i].Location = new Point(0, 0);
   picNewMeatLoaf[i].Size = new Size(75, 25);
   picNewMeatLoaf[i].BringToFront();
   picNewMeatLoaf[i].Image = this.picMeatLoaf.Image;
  this.Controls.AddRange(new Control[] { picNewMeatLoaf[i] });


	this.ResumeLayout();

___________________________________
(in a timer)
 for (int j = 1; j < 20; j++)
		 {
			 if (this.picNewMeatLoaf[j].Top < 305 & picNewMeatLoaf[j].Left < 693)	//305 is the ground level, 693 is the spot on Hilary's face to stop
			 {
				 this.picNewMeatLoaf[j].Top = picNewMeatLoaf[j].Top + 1;
				 this.picNewMeatLoaf[j].Left = picNewMeatLoaf[j].Left + MeatLoafSpeed[j];
			 }

That is just the code related to the MeatLoaf picturebox, so obviously there is a lot more code. Note, this is my first real attempt at a program with C#, so the code isn't going to be the best.

Does anyone see what I'm doing wrong or have any suggestions? I'm all out of ideas, and I'd rather not have to just add it into the form1's background image. If you need anymore code or information, just let me know.

Oh, and if anyone has any ideas on how to make parts of picture boxes transparent, meaning they show the objects behind it, it would be VERY appreciated.

Pierce

1 answer to this question

Recommended Posts

  • 0

Try using .Visible instead of BringToFront()

ie.

picNewMeatLoaf[j].Visible = false; // The image you wanna hide

picNewMeatLoaf.Visible = true; // The image you wanna show

Also, you can try putting them in small containers, such as panels if you still have trouble and use panel1.BringToFront() or Panel1.Visible = true and panel2.Visible = false;

To make the image transparent, try opening it in paint or something and selecting the area you want to be transparent and delete it and save and re-import into visual studio.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.