我正在编写一个简单的水果机器,这是我的方法之一,我想知道是否有可能使这个代码更有效:(在使用案例之前我有一个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];
}
答案 0 :(得分:0)
将您的图片放在应用程序旁边的文件夹中,然后调用pictureBox NudgeCount.Load(ConstantSectionPath + _intNudgeCount +“。jpg”);如果jpg是你的文件扩展名。
答案 1 :(得分:0)
我会把它减少到只有一行代码我就是你了!
pictureBoxNudgeCount.Image = (Image)Properties.Resources.ResourceManager.GetObject("_" + _intNudgeCount);