C# - 我似乎无法在 windows 表单的标签上逐行显示 10 个数字的平方结果

C# - I can't seem to have the results of the square of 10 numbers displayed on a label in a windows Form line by line

我有一个带有按钮和标签的 window 表单应用程序。 按下按钮后,标签需要显示数字 1 到 10 的平方。这需要逐行显示。 这是我到目前为止的代码:

   private void button1_Click(object sender, EventArgs e)
    {
        int[] sqrNumbers = new int[10];
        for (int i = 1; i != sqrNumbers.Length; i++)

        {

            sqrNumbers[i] =  i * i;

            lblSquares.Text = (sqrNumbers[i]).ToString()+ Environment.NewLine;

        }

不幸的是,这不起作用。由于我是编程新手,我似乎无法弄清楚这一点。我在网上搜索以找到答案,但没有找到我要找的东西。谢谢你帮我解决这个问题。

氪,

周杰伦

试试这个:(编辑这个可能更安全)

  private void button1_Click(object sender, EventArgs e)
    {
    int[] sqrNumbers = new int[10];
    String text = "";
    for (int i = 1; i != sqrNumbers.Length; i++)

    {

        sqrNumbers[i] =  i * i;

        text += (sqrNumbers[i]).ToString()+ Environment.NewLine;

    }

    lblSquares.Text = text;
 }
 lblSquares.Text = (sqrNumbers[i]).ToString()+ Environment.NewLine;

覆盖lblSquares.Text

的值
lblSquares.Text += (sqrNumbers[i]).ToString()+ Environment.NewLine;

将保留值并添加到它。

https://msdn.microsoft.com/en-us/library/sa7629ew.aspx