【问题标题】:Visual Studio 2012 Dynamic PictureBox ImageVisual Studio 2012 动态图片框图像
【发布时间】:2015-02-01 06:22:18
【问题描述】:

我有一个简单的问题,我一直在努力解决理论上应该很简单的事情。

我想在 Visual Studio 2012 的图片框中创建一个动态按钮,每次单击它都会更改图像。

if (pictureBox4.BackgroundImage == MyProject.Properties.Resources._1)
    pictureBox4.BackgroundImage = MyProject.Properties.Resources._2;
else if (pictureBox4.BackgroundImage == MyProject.Properties.Resources._2)
    pictureBox4.BackgroundImage = MyProject.Properties.Resources._1;

现在,这并不能正常工作。它不会检测到当前正在显示的图像并输入if 语句。所以,相反,我以这种方式对其进行了测试。

int b = 1; 

if (b == 1)
{
    pictureBox4.BackgroundImage = MyProject.Properties.Resources._2;
    b = 2;
}

if (b == 2)
{
    pictureBox4.BackgroundImage = MyProject.Properties.Resources._1;
    b = 1;
}

关门……但没有雪茄。当我点击它时,图像确实会改变,但只有一次;如果我再次单击它,它会保持不变...

那么……现在呢?感谢您的回答。

【问题讨论】:

  • 第一行 (int b = 1;) 是在您的事件处理程序方法中还是对类来说是全局的?
  • global,在课堂上,对不起,我忘了说明,把它放在事件处理程序中会很愚蠢......呵呵
  • 在每个 if 之前添加一个 else 子句,否则它将不起作用 ;-) (因为第一个更改将使第二个更改为 true 并且它会恢复该更改等。)
  • 你真的在两个 if 子句中都使用了这一行吗? pictureBox4.BackgroundImage = MyProject.Properties.Resources._2;

标签: c# visual-studio visual-studio-2012 picturebox


【解决方案1】:

这是我的任务解决方案:在MyProject 中,我有一个名为Resource1 的资源文件和从_0_5 的六个Images

int index = 0;
private void anImageButton_Click(object sender, EventArgs e)
{
    index = (index + 1) % 6;
    anImageButton.Image = 
          (Bitmap)MyProject.Resource1.ResourceManager.GetObject("_" + index);
}

我使用简单的Button anImageButton 而不是PictureBox,但它也适用于您的PictureBox..

【讨论】:

    【解决方案2】:

    您在两个 if 子句 (Resources._2) 中使用相同的图像?

    另外,正如 TaW 所指出的,当 b == 1 时,您设置 b = 2 并检查 b == 2。两个 if 子句都为真。第二个 if 应该是“else if”

        if (b == 1)
        {
            pictureBox4.BackgroundImage = MyProject.Properties.Resources._2;
            b = 2;
        }
        else if (b == 2)
        {   
            pictureBox4.BackgroundImage = MyProject.Properties.Resources._1;
            b = 1;
        }
    

    当然你可以只使用 else 子句:

        if (b == 1)
        {
            pictureBox4.BackgroundImage = MyProject.Properties.Resources._2;
            b = 2;
        }
        else
        {   
            pictureBox4.BackgroundImage = MyProject.Properties.Resources._1;
            b = 1;
        }
    

    或者如果你不想要整数变量,那么你可以试试这个:

    1) 只从资源中拉取图像一次,并将它们放在一个静态成员中。

        private static readonly Image Image1 = MyProject.Properties.Resources._1;
        private static readonly Image Image2 = MyProject.Properties.Resources._2;
    

    2) 更改条件以使用静态副本而不是资源属性(每次都返回一个新对象)。

        if (pictureBox4.BackgroundImage == Image2)
        {
            pictureBox4.BackgroundImage = Image1;
        }
        else
        {
            pictureBox4.BackgroundImage = Image2;
        }
    

    【讨论】:

    • 可能是一种类型。更重要的是,他缺少 else 子句!
    • 是的,对不起,我的意思是 Resources._1 我的错......现在,奇怪的是,我再次运行代码,它根本不起作用,甚至第一次点击都没有......
    • 呵呵。再次阅读我们的 cmets ;-)
    • 你用 else 替换了第二个 if? (请参阅我编辑的答案和来自 TaW 的评论。)如果您错过了,那么它根本不会更新。
    • 好吧,我太傻了,没有把 else 放在那里……解决了问题!所以它起作用了...... buuuuut,我将把其中的许多放在一行中,我不能为每个整数创建 20 个整数,有什么想法为什么第一种方法不起作用??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-17
    • 1970-01-01
    • 2011-01-10
    • 1970-01-01
    • 2012-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多