鼠标单击后显示图片框的内容

Show content of a picturebox after mouseclick

我试图在单击图片后在 pictureBox 中显示图片。

首先,我尝试将 PictureBox 属性 的可见性设置为 false,在我单击 PictureBox 之后,这些选项变为 true,并且应该显示图片。 这没用。 请告诉我此操作的最佳实践是什么。

这是我的 PictureBoxes 列表

List<PictureBox> logos = new List<PictureBox>();
//Here how I put Images into every single PictureBox:

 private void f1()
    {
        Insert_Logo();
        PictureBox picture;
        int randomnumber;

        for(int i=0; i< tableLayoutPanel1.Controls.Count; i++)
        {
            if (tableLayoutPanel1.Controls[i] is PictureBox)
            {                      
                    picture = (PictureBox)tableLayoutPanel1.Controls[i];
            }
            else
                continue;

            randomnumber = random.Next(0, logos.Count);
            picture.Image = logos[randomnumber].Image;             
            logos.RemoveAt(randomnumber);                
        }

    }

这是我在点击 PictureBox 后尝试显示图像的函数:

    private void PictuteBox_CLICK(object sender, EventArgs e)
    {

        PictureBox clickedPicture = sender as PictureBox;

        if(clickedPicture == null)
        {
            return;
        }

        if (firsPic == null)
        {
            firsPic = clickedPicture;
            firsPic.Visible = true;
            return;
        }

    }    

这是在 PictureBox 中插入图片的代码:

       private List<PictureBox> Insert_Logo()
    {
        PictureBox pic1 = new PictureBox();
        Image image = Image.FromFile("D:\Project\Mini-Game\Mini-Game\bin\Image\Renault.bmp");
        pic1.Image = image;

        PictureBox pic2 = new PictureBox();
        Image a = Image.FromFile("D:\Project\Mini-Game\Mini-Game\bin\Image\vw.bmp");
        pic2.Size = new Size(a.Width, a.Height);
        pic2.Image = a;

        PictureBox pic3 = new PictureBox();
        Image s = Image.FromFile("D:\Project\Mini-Game\Mini-Game\bin\Image\alfa.bmp");
        pic3.Size = new Size(s.Width, s.Height);
        pic3.Image = s;

        logos.Add(pic1);
        logos.Add(pic2);
        logos.Add(pic3);
        logos.Add(pic4);
        logos.Add(pic5);
        logos.Add(pic6);

return logos;

我认为你应该这样写你的代码,解释有注释

List<PictureBox> logos = new List<PictureBox>();   
private List<PictureBox> Insert_Logo()
{
    PictureBox pic1 = new PictureBox();
    Image image = Image.FromFile("D:\Project\Mini-Game\Mini-Game\bin\Image\Renault.bmp");
    pic1.Image = image;

    PictureBox pic2 = new PictureBox();
    Image a = Image.FromFile("D:\Project\Mini-Game\Mini-Game\bin\Image\vw.bmp");
    pic2.Size = new Size(a.Width, a.Height);
    pic2.Image = a;

    PictureBox pic3 = new PictureBox();
    Image s = Image.FromFile("D:\Project\Mini-Game\Mini-Game\bin\Image\alfa.bmp");
    pic3.Size = new Size(s.Width, s.Height);
    pic3.Image = s;

    logos.Add(pic1);
    logos.Add(pic2);
    logos.Add(pic3);
    return logos;
}

private void PictuteBox_CLICK(object sender, EventArgs e)
{

    logos = Insert_Logo();
    //The Insert_Logo function returns a list of pictureboxes.
    int randomnumber;
    randomnumber = random.Next(0, logos.Count);
    //Replace Picturebox with the name of the picturebox you want to show the image in
    Picturebox.Image = logos[randomnumber].Image;
    logos.RemoveAt(randomnumber); 
}