【问题标题】:Change multiple images in list with one button一键更改列表中的多个图像
【发布时间】: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


    【解决方案1】:

    您必须遍历数组。然后在 for 循环中更改图像。

        /// <summary>Update the image list with random dice images.</summary>
        public static void UpdateDices()
        {
            // Iterate through the image array.
            for (var i = 0; i < imgList.Length; i++)
            {
                // Update the dice image.
                imgList[i] = GetRandomDice();
            }
        }
    

    【讨论】:

    • 我尝试了你的建议,但没有骰子(呵呵)。不过,这给了我一些想法,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-08
    • 2013-09-27
    • 2020-07-30
    • 2019-04-02
    • 1970-01-01
    相关资源
    最近更新 更多