StringBuilder to textBox help

  • Thread starter Thread starter tiger
  • Start date Start date
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
 
Tiger,

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

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

HTH

Ollie Riches
 
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.
 
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
 
Back
Top