Ebikeneser
Ebikeneser

Reputation: 2364

Add spacing to textbox results

Hi there I have the following code-

        richTextBox1.Text = richTextBox1.Text + action + "ok: " + ok.ToString();
        richTextBox1.Text = richTextBox1.Text + "err: " + err.ToString();
        richTextBox1.Text = richTextBox1.Text + "\r\n";
        textBox1.Text = textBox1.Text;

The results look like -

ok:7err:0

But I want-

ok:7

err:0

With spacing, to make it look better how can I do this?

Upvotes: 0

Views: 1677

Answers (3)

ChrisF
ChrisF

Reputation: 137148

You could add another 2 lines:

richTextBox1.Text += Environment.NewLine;
richTextBox1.Text += Environment.NewLine;

between your "ok" and "err" - assuming you want a blank line between the two lines of output. However, you should either be using string.Format or a StringBuilder to create your output as concatenating strings this way in inefficient.

You also don't need the final:

textBox1.Text = textBox1.Text;

as that is just setting the text box contents back to itself and does nothing.

Upvotes: 3

atoMerz
atoMerz

Reputation: 7672

first that you could do all these in a single statement, second you could use += operator instead, and third what is that last statement doing?! it not needed, fourth add "\n" after each part you need there is no limit where you should put it, no "\r" needed.

Upvotes: 1

Cody Gray
Cody Gray

Reputation: 244752

You've already got your answer, you just have it in the wrong place! The key is to use the escape sequence \r\n, which inserts a carriage return and a new line.

Also, there's no reason to split this code up into multiple lines. You end up incurring a performance penalty for doing so. It's better to do all of the string concatenation at one time. (You aren't doing enough concatenations here to justify using the StringBuilder class, but it's worth keeping in mind that strings are immutable in .NET and writing code accordingly.)

Try rewriting the code like this:

textBox1.Text = textBox1.Text + action + "ok: " + ok.ToString(); + "\r\n" +
                "err: " + err.ToString(); + "\r\n";

You can also complete eliminate the last line of code, as that simply sets the value of textBox1.Text to itself. It's a no-op, meaning that it does nothing at all.

Upvotes: 1

Related Questions