3

I'm trying to create a table with data using StringBuilders AppendFormat method. I want the text in the second and third column to be left aligned.

Here is an example I found on google: http://www.csharp-examples.net/align-string-with-spaces/

However, using the same code as the website I get different results. The placement of the second column text depends on the length of the first column text. Am I doing this wrong? Is there a better method that would be more suited for this task? I want a certain amount of space from the first character of the first column to the first character of the second column ( like the setw() method in c++)

symbolTableToString.AppendFormat(" {0,-20}  {1,-10}  {2,-10} {3,-10}",dataNode.token, dataNode.tokenClass, dataNode.tokenMode, dataNode.alias).AppendLine();

I'm putting each line into a text box and am unable to use a different widget as this is part of a homework assignment. enter image description here

3
  • try setting the text box to have a fixed width font like Courier. Commented Jun 9, 2012 at 17:06
  • @hatchet Thats it! thank you! Post it as an answer so I can accept the answer :) Commented Jun 9, 2012 at 17:08
  • You should use a DataGridView. Commented Jun 9, 2012 at 17:09

2 Answers 2

3

Your textbox has a variable width font, so using counts of spaces to align columns will almost never turn out well. Change the Font property of the textbox to a fixed width font such as Courier New.

Sign up to request clarification or add additional context in comments.

Comments

0

Your problem isn't the AppendFormat, that actually seems to work correctly. Set that text box to use a monospaced font, e.g. Courier New or Consolas.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.