-1

我正在编写一个简单的水果机,这是我的方法之一,我想知道是否可以使这段代码更高效:(在使用案例之前,我有一个 if / else if 语句)

_intNudgeCount 得到一个介于 0 - 9 之间的数字,因此是这种情况。

public void DrawNudgeCount()
    {
        switch (_intNudgeCount)
        {
            case 9:
                pictureBoxNudgeCount.Image = Properties.Resources._9;
                break;
            case 8:
                pictureBoxNudgeCount.Image = Properties.Resources._8;
                break;
            case 7:
                pictureBoxNudgeCount.Image = Properties.Resources._7;
                break;
            case 6:
                pictureBoxNudgeCount.Image = Properties.Resources._6;
                break;
            case 5:
                pictureBoxNudgeCount.Image = Properties.Resources._5;
                break;
            case 4:
                pictureBoxNudgeCount.Image = Properties.Resources._4;
                break;
            case 3:
                pictureBoxNudgeCount.Image = Properties.Resources._3;
                break;
            case 2:
                pictureBoxNudgeCount.Image = Properties.Resources._2;
                break;
            case 1:
                pictureBoxNudgeCount.Image = Properties.Resources._1;
                break;
            case 0:
                pictureBoxNudgeCount.Image = Properties.Resources._0;
                break;
        }
    }

提前致谢!

解决了:

没关系,我把它简化为 3 行代码:

//在类的顶部声明资源图像。

private System.Drawing.Image[]  _arrayNudgeCount;

//加载类时填充数组。

_arrayNudgeCount = new System.Drawing.Image[] { Properties.Resources._0, Properties.Resources._1};

//重绘图片

public void DrawNudgeCount()
{
pictureBoxNudgeCount.Image = _arrayNudgeCount[_intNudgeCount];
}
4

2 回答 2

0

I would have reduced it to just one line of code I were you!

pictureBoxNudgeCount.Image = (Image)Properties.Resources.ResourceManager.GetObject("_" + _intNudgeCount);
于 2013-04-21T04:00:11.487 回答
0

将您的图像放在应用程序旁边的文件夹中并调用 pictureBoxNudgeCount.Load(ConstantSectionPath+_intNudgeCount+".jpg"); 如果 jpg 是您的文件扩展名。

于 2013-04-20T20:13:49.177 回答