Picturebox Grid For Loop

时间:2016-05-03 22:45:23

标签: c#

我正在制作一个32x32的图片框,它的宽度和高度均为20像素。我也为它们中的每一个设置了一个随机颜色,我将颜色存储在一个字符串数组中,颜色的Hex信息。当我开始游戏时,一切都很完美,这很棒:)但是这是耗时的grrr ..我如何改进我的方法或算法来检索更快的结果?这是我的代码:

public int getRandColour()
{
    tryAgain:
        Random rnd = new Random();
        int randInt = rnd.Next(0, 8);
        if (randInt == previousColour)
        {
            goto tryAgain;
        }
        return randInt;
}
public void makeGrid()
{
    loadBar.Maximum = setSize * setSize;
    for (int x = 0; x < setSize; x++)
    {
        for (int y = 0; y < setSize; y++)
        {
            grid[x, y] = new PictureBox();
            grid[x, y].Tag = (Object)x;
            grid[x, y].Name = y.ToString();
            grid[x, y].Width = 20;
            grid[x, y].Height = 20;
            grid[x, y].Location = new Point(x * 20 + 20, y * 20 + 20);
            grid[x, y].Visible = true;

            currentColour = getRandColour();
            grid[x, y].BackColor = ColorTranslator.FromHtml(colours[currentColour]);

            gridColour[x, y] = currentColour;
            previousColour = currentColour;

            grid[x, y].BorderStyle = BorderStyle.None;
            grid[x, y].BringToFront();
            grid[x, y].Click += new System.EventHandler(this.paintBoxes);
            this.Controls.Add(grid[x, y]);
            loadBar.Value++;
        }
    }
    loadBar.Visible = false;
}

我是否可以在getRandColour()函数中实现某种算法,以便更快地运行并确保最后一个int与新int不同?

0 个答案:

没有答案