Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在单击按钮时将一个图像更改为另一个图像_C# - Fatal编程技术网

C# 如何在单击按钮时将一个图像更改为另一个图像

C# 如何在单击按钮时将一个图像更改为另一个图像,c#,C#,我有两个图像,使用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

我有两个图像,使用
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      
   }
}

你可以这样做:

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

您可以使用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;
}

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

这个问题太基本了。找到一个工作示例(我假设是WinForms?)并首先尝试一下。B.t.w,if语句具有赋值运算符(=);这应该是“==”。看看有类似问题的问题。你必须使用visibility属性。与其检查bitmap属性,不如使用简单的布尔标志。它不起作用,当我单击按钮时,没有任何变化,我复制了和你完全一样的东西。@LokeBoon你不应该只复制我的答案而不仔细阅读。那个的主要思想是使用标志来保存状态。对不起,我对编程很陌生,所以我不知道什么是标志或状态。。我确实仔细考虑了一下,并尝试了其他的选择,但没有结果。。