【发布时间】:2018-12-17 15:16:07
【问题描述】:
编程新手。我正在为学校的 Petals Around the Rose 游戏开发 Windows 表单应用程序,虽然我们的讲师已经向我们展示了如何在单击时更改单个图片框,但我想制作一个可以一次更改所有图片框的按钮/ '重掷'骰子。
一些代码:
private void frmDisplay_Load(object sender, EventArgs e)
{
// Create list object to hold dice images
diceArray = new List<MyPictureBox>();
for (int i = 0; i < 5; i++)
{
MyPictureBox picBox = new MyPictureBox(i + 1);
picBox.Image = RandomDice.GetRandomDice();
picBox.Click += new System.EventHandler(pictureBox_Click);
this.Controls.Add(picBox);
this.Controls.Add(picBox.TheLabel);
}
}
上面的代码在表单加载时创建了 5 个图片框,并从我的 RandomDice 类中提取,这里:
class RandomDice
{
private static Random randomGen = new Random();
private static Image[] imgList = new Image[6];
public static Image GetRandomDice()
{
int i = 0;
ResourceSet rsrcSet = Properties.Resources.ResourceManager.GetResourceSet(CultureInfo.CurrentCulture, true, true);
foreach (DictionaryEntry entry in rsrcSet)
{
imgList[i] = (Image) entry.Value;
i++;
}
i = randomGen.Next(0, imgList.Length);
return imgList[i];
}
}
我为按钮点击设置了一个事件,但我不知道如何实现,所以按钮会立即更改我的所有骰子图像。
有什么建议吗?
谢谢!
【问题讨论】:
标签: c# winforms list button picturebox