简体   繁体   English

单击按钮后如何将一个图像更改为另一个图像

[英]How do I change one image to another upon clicking on a button

I have 2 images, using the else-if method, how do I make it so that when I click on image A, it turns to image B and if its image B, turn back to image A? 我有2张图片,使用else-if方法,如何制作它,以便当我单击图片A时,它变成图片B,如果点击图片B,又回到图片A? Here is my codes: 这是我的代码:

private void Smalltubbutton1_Click(object sender, EventArgs e)
{
   if(System.Drawing.Bitmap bitmap1 =
      WindowsFormsApplication21.Properties.Resources.smalltub)
   {
         System.Drawing.Bitmap bitmap1 = 
                        WindowsFormsApplication21.Properties.Resources.GRAYSCALEsmalltub;
   }
   else
   {
     System.Drawing.Bitmap bitmap1 = 
                    WindowsFormsApplication21.Properties.Resources.smalltub      
   }
}

You could do that someway like this: 您可以这样来做:

private bool flag;
private void Smalltubbutton1_Click(object sender, EventArgs e)
{
    System.Drawing.Bitmap bitmap1;

    if (flag)
    {
        bitmap1 = WindowsFormsApplication21.Properties.Resources.GRAYSCALEsmalltub;
    }
    else
    {
        bitmap1 = WindowsFormsApplication21.Properties.Resources.smalltub      
    }

    flag = !flag;
}

You can use a PictureBox and change the image property with your proper image and change a boolean property or variable each time you click on that. 您可以使用PictureBox并使用适当的图像更改image属性,并在每次单击该属性时更改一个布尔属性或变量。

private void pictureBox1_Click(object sender, EventArgs e)
{
      if (flag)
           pictureBox1.Image = WindowsFormsApplication21.Properties.Resources.GRAYSCALEsmalltub;
      else
           pictureBox1.Image = WindowsFormsApplication21.Properties.Resources.smalltub;
      flag=!flag;
}

I hope this would solve your problem. 我希望这可以解决您的问题。 Good Luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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