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

时间:2015-07-26 06:52:58

标签: c#

我有2张图片,使用else-if方法,如何制作,以便当我点击图片A时,它会转到图像B,如果是图像B,则转回图像A?这是我的代码:

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      
   }
}

2 个答案:

答案 0 :(得分:2)

你可以这样做:

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;
}

答案 1 :(得分:2)

您可以使用PictureBox并使用正确的图像更改图像属性,并在每次单击时更改布尔属性或变量。

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;
}

我希望这能解决你的问题。祝你好运。