开发者

Why do I only see the last value from a List in my RichTextBox?

Why is this code only outputting one element from the list?

private void button3_Click(object sender, EventArgs e)
    {
        Tasks dosomething = new dosomething();
        dosomething.calculate();

        foreach(float num in dosomething.Display())
        {
            richTextBox1.Text = num.ToString();
        }   
    }

Where dosomething.Display() is retur开发者_如何学运维ning a List<float> list; with 10 floats.


You need to use

richTextBox1.Text += num.ToString();

By what you are doing, only the very last item will be displayed.

A better way would be to make use of a StringBuilder

StringBuilder sb = new StringBuilder();
            foreach (float num in dosomething.Display()) 
            { 
                sb.Append(num.ToString() + Environment.NewLine);
            } 
            richTextBox1.Text = sb.ToString(); 


Each time the running code enters the foreach loop,

richTextBox1.Text = num.ToString();

clears the richTextBox1 and inserts num.ToString() into it. Next time the code in that foreach loop is executed, the contents are once again replaced.

I think that you are trying to append results here. There are many ways you could do this. For example, you could easily refactor that previous statement to:

richTextBox1.Text += num.ToString();

Note the += operator: += means append to. Thus, richTextBox1.Text = richTextBox1.Text + num.ToString();

Of course, you could also add whitespace to make it look pretty...

richTextBox1.Text += num.ToString()+" ";

Or, as astander suggested, you could use a StringBuilder. A StringBuilder is a very efficient class for manipulating strings without the ordinary memory penalties that doing this with regular string classes involves. In addition, the following code below (which is very similar to astander's code, as they essentially function the same way) only updates the richTextBox1 on the user interface once, rather than once for each element of the list.

StringBuilder code:

StringBuilder sbuilder = new StringBuilder(); //default StringBuilder() constructor
foreach (float num in dosomething.Display())
{
sb.Append(num.ToString() + " ");
}
//Foreach loop has ended - this means all elements of the list have been iterated through
//Now we set the contents of richTextBox1:
richTextBox1.Text = sb.ToString();

Note that in the foreach loop, we are appending to the StringBuilder, via the StringBuilder.Append() method.

Then, of course, you could, once again, change what appears between outputted floats. I just put a space in (" ").

So, to answer your question, only one appears because you are setting the value each time, rather than appending. When you set a value, the previous value is discarded.

Hope I helped!

P.S. Just saw a curious little thing in your code:

Tasks dosomething = new dosomething();

Are you sure that it wouldn't be:

Tasks dosomething = new Tasks();

But that's not related to your question.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜