【发布时间】:2020-04-12 02:17:00
【问题描述】:
我的应用程序中有 5 个图片框,我同时更新它们。 我希望他们显示 5 个不同图像,我的程序代码是正确的,但由于某种原因,图片框显示所有相同的图像...
代码如下:
private System.Drawing.Bitmap DiceOne;
private System.Drawing.Bitmap DiceTwo;
private System.Drawing.Bitmap DiceThree;
private System.Drawing.Bitmap DiceFour;
private System.Drawing.Bitmap DiceFive;
private System.Drawing.Bitmap DiceSix;
public void Prepare()
{
for (int i = 0; i < 5; i++)
Dices[i] = new Dice();
DiceOne = Properties.Resources.Würfel_1;
DiceTwo = Properties.Resources.Würfel_2;
DiceThree = Properties.Resources.Würfel_3;
DiceFour = Properties.Resources.Würfel_4;
DiceFive = Properties.Resources.Würfel_5;
DiceSix = Properties.Resources.Würfel_6;
}
public void Roll()
{
foreach (Dice dice in Dices)
dice.RollTheDice();
View.SuspendLayout();
View.diceOnePictureBox.Image = DiceNumberToBmp(Dices[0].GetLastRolled());
View.diceOnePictureBox.Update();
View.diceTwoPictureBox.Image = DiceNumberToBmp(Dices[1].GetLastRolled());
View.diceOnePictureBox.Update();
View.diceThreePictureBox.Image = DiceNumberToBmp(Dices[2].GetLastRolled());
View.diceOnePictureBox.Update();
View.diceFourPictureBox.Image = DiceNumberToBmp(Dices[3].GetLastRolled());
View.diceOnePictureBox.Update();
View.diceFivePictureBox.Image = DiceNumberToBmp(Dices[4].GetLastRolled());
View.diceOnePictureBox.Update();
View.ResumeLayout(false);
}
private System.Drawing.Bitmap DiceNumberToBmp(int number)
{
switch(number)
{
case 1:
return DiceOne;
case 2:
return DiceTwo;
case 3:
return DiceThree;
case 4:
return DiceFour;
case 5:
return DiceFive;
case 6:
return DiceSix;
default:
return null;
}
}
我之前在互联网上阅读过一些比较熟悉的帖子,并尝试使用 SuspendLayout、ResumeLayout、更新 PictureBox 来解决......对我没有任何帮助。
我的骰子课:
using System;
namespace KniffelGUI.Model
{
public class Dice
{
private int LastRolled;
public Dice()
{
RollTheDice();
}
public Dice(int fakeRoll)
{
LastRolled = fakeRoll;
}
public int RollTheDice()
{
LastRolled = new Random().Next(6) + 1;
return LastRolled;
}
public int GetLastRolled()
{
return LastRolled;
}
}
}
【问题讨论】:
-
你在
Dice.GetLastRolled()中使用Random吗? -
哦好吧,没注意,改成各自的名字,还是不行。
-
@BarryO'Kane 在 GetLastRolled() 中我只返回一个局部变量,但在 Dice.RollTheDice() 中我使用的是 Random,是的。
-
你能发布那个代码吗?听起来您可能是 Random 默认构造函数问题csharpindepth.com/Articles/Random 的受害者
-
@BarryO'Kane 随机作品,它返回我从 1 到 6 的数字。这只是图片框的问题
标签: c# bitmap picturebox