切换按钮有加载错误

时间:2014-08-14 08:15:37

标签: c# picturebox togglebutton

我们为switch(toggle)按钮编写了这段代码。 我们正在使用WindowsForm

int i=0;
private void pictureBox1_Click(object sender, EventArgs e)
{
    ++i; 

    if (i == 1)
    {
        pictureBox1.Image = Image.FromFile("D:\\off.png"); 
    } 
    else if (i == 2) 
    {
        pictureBox1.Image = Image.FromFile("D:\\on.png");
        i = 0; 
    }
}

它有效但我们看到加载光标。原因是什么?

image size like this

1 个答案:

答案 0 :(得分:0)

看起来它可能是你的代码如何工作的可能性,所以让我给你一个更好的代码,应该做到这一点,并将更有效。

bool IsOn = true;

private void pictureBox1_Click(object sender, EventArgs e)
{

    if(IsOn)
    {
      pictureBox1.Image = Image.FromFile("D:\\off.png"); 
      IsOn = false;
    } else {
      pictureBox1.Image = Image.FromFile("D:\\on.png");
      IsOn = true;
    } 
}

我希望这有帮助!

编辑:我实际上误解了这一点,但请随意使用上面的代码。

确保您的文件都存在,因为这种错误表明文件不存在。