C#随机数Gen在12次点击后停止

时间:2015-02-10 05:25:10

标签: c# random

在完成作业分配和随机数生成器停止在12个按钮点击后填充TestBox?每次运行它都会发生这种情况,我不明白为什么。如果有什么看起来马虎,请说出来。

public partial class GuessANumber : Form
{
    int[] array = new int[100];
    public GuessANumber()
    {
        InitializeComponent();
        array = RandomArray();
        this.Gen_Rand.Click += new EventHandler(this.Gen_Rand_Click);
    }

    public int[] RandomArray()
    {            
        Random rand = new Random();
        for (int i = 0; i < array.Length; i++)
        {
            array[i] = rand.Next(1, 100);
        }
        return array;
    }

    private void Form1_Load(object sender, EventArgs e)
    {}

    private void Gen_Rand_Click(object sender, EventArgs e)
    {
        array = RandomArray();
        for (int i = 0; i < array.Length; i++)
        {
            TestBox.AppendText(" " + Convert.ToString(array[i]));
        }
    }
}

1 个答案:

答案 0 :(得分:2)

在这一行

TestBox.AppendText(" " + Convert.ToString(array[i]));

追加越来越多的文字到TextBox。如果TextBox未调整其大小或滚动条,则您将看不到文本的最新部分。

添加类似

的内容
TestBox.Clear();

Gen_Rand_Click()

的开头
相关问题