Why do I only see some of my text output when my W

2020-05-09 18:17发布

Im trying to provide two numbers, a maximum and minimum, to create a multiplication table. My current code works perfectly in console, but I cant get it to properly print to a text box in Windows Forms Applications. I only manage to print a single, maximum, number.

private void MultiTab_CheckedChanged(object sender, EventArgs e)
{
    string final = "The Multiplication Table Is:";
    int i = Convert.ToInt32(Max.Text);
    int k = Convert.ToInt32(Min.Text);

    for (i = 1; i <= k; i++)
    {
        Result.Text = (i + "\t");
        for (int j = 1; j <= k; j++)
        {
            string x;

            if (i > k)  x = i * j + "\t";
            else x = j + "\t";
            Result.Text = final + x;
        }
        Result.Text = ("\n");
    }
}

1条回答
三岁会撩人
2楼-- · 2020-05-09 18:52

Result.Text = will replace the entire string, including newlines.

You need to add to Result.Text instead of replacing it.

Before the for loop, initialize it to an empty string:

Result.Text = "";

Then below that point, always append rather than replacing, e.g.

Result.Text += (i + "\t");

Instead of

Result.Text = (i + "\t");

And so on.

查看更多
登录 后发表回答