我有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
}
}
答案 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;
}
我希望这能解决你的问题。祝你好运。