StringBuilder to textBox help

T

tiger

Hi,

I am runing into problems with the following code, What I am trying to do is
display the object (sb) in a textBox, rather than MessageBox. This is even
possible?

StringBuilder sb = new StringBuilder();

sb.Append("Payment paid to: " + myPayment.Payor);
sb.Append("\n");
sb.Append("Paid by: " + myPayment.Payee);
sb.Append("\n");
sb.Append("Amount: $" + myPayment.Amount.ToString());
sb.Append("\n");
sb.Append("Due Date: " + Convert.ToDateTime(myPayment.DueDate));

MessageBox.Show(sb.ToString(), "Message Received!");

Your assistants will be greatly appreciated.

Thanks
tiger
 
C

Cor Ligthert [MVP]

Tiger,

What is the problem, (did you set the textbox to multiline is true?)

Cor
 
O

Ollie Riches

on top of what Cor says you should be using Environment.NewLine instead of
"\n"...

HTH

Ollie Riches
 
J

Jon Skeet [C# MVP]

tiger said:
I am runing into problems with the following code, What I am trying to do is
display the object (sb) in a textBox, rather than MessageBox. This is even
possible?

StringBuilder sb = new StringBuilder();

sb.Append("Payment paid to: " + myPayment.Payor);
sb.Append("\n");
sb.Append("Paid by: " + myPayment.Payee);
sb.Append("\n");
sb.Append("Amount: $" + myPayment.Amount.ToString());
sb.Append("\n");
sb.Append("Due Date: " + Convert.ToDateTime(myPayment.DueDate));

MessageBox.Show(sb.ToString(), "Message Received!");

Your assistants will be greatly appreciated.

In addition to the other comments, your code is less efficient than it
might be. Even though you're using a StringBuilder, you're still
creating temporary strings unnecessarily. In fact, using straight
string concatenation would be more efficient in this case:

string message = "Payment paid to: " + myPayment.Payor +
Environment.NewLine +
"Paid by: " + myPayment.Payee +
Environment.NewLine +
"Amount: $" + myPayment.Amount +
Environment.NewLine +
"Due Date: " + Convert.ToDateTime(myPayment.DueDate);

That does it all in one call to String.Concat, and is probably the most
efficient way. Alternatively, you could still use a StringBuilder, but
call Append with "Payment paid to: ", then call Append again with
myPayment.Payor etc.

See http://www.pobox.com/~skeet/csharp/stringbuilder.html for more
information.
 
K

Ken Wilson

Hi,

I am runing into problems with the following code, What I am trying to do is
display the object (sb) in a textBox, rather than MessageBox. This is even
possible?

StringBuilder sb = new StringBuilder();

sb.Append("Payment paid to: " + myPayment.Payor);
sb.Append("\n");
sb.Append("Paid by: " + myPayment.Payee);
sb.Append("\n");
sb.Append("Amount: $" + myPayment.Amount.ToString());
sb.Append("\n");
sb.Append("Due Date: " + Convert.ToDateTime(myPayment.DueDate));

MessageBox.Show(sb.ToString(), "Message Received!");

Your assistants will be greatly appreciated.

Thanks
tiger
It is quite possible. Make sure your TextBox has the 'Multiline'
property set to true. Assuming your variable name for the TextBox is
'myTextBox', your code should read as follows;

myTextBox.Text = sb.ToString();


Ken Wilson
Seeking viable employment in Victoria, BC
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top