简体   繁体   中英

How to add a picturebox above a button control

I have a picturebox1 -> Button -> picturebox2 all three are in a consecutive layer so which I want is that picturebox2 should be appear within the button when I debug the program.

My code is,

public Form1()
    {
        InitializeComponent();
        picturebox2.parent = button;
        picturebox.backcolor = color.transparent;
     }

I am using .jpg for picturebox1 and a .png for picturebox2 but its not appearing. I mean the picture of picturebox2 should appear above the button.

  • You need to nest all 3 controls.

  • You also need to correct the Location of the nested controls or else they keep the original location, which are relative to their original parents, probably to the form, and not to their new parents!!

This should work better:

public Form1()
{
    InitializeComponent();
    button.Parent = picturebox;
    picturebox2.Parent = button;
    picturebox.BackColor = Color.Transparent;

    button.Location = new Point(1,2);      // or whatever you want!!
    picturebox2.Location = new Point(3,4); // or whatever you want!!
 }

You may also want to consider simply using the Image and/or the BackGroundImage properties of the Button ..

Note: If you want your Button to let the bottom PictureBox shine through you need to not only set its Color but also it's FlatStyle to Flat !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM