简体   繁体   中英

Align N columns of single characters

I am attempting to have N number of columns of single characters in C# by outputting a string through a label for each row in WFA. I attempted to use the solution found Here using String.Format but I can't since I do not know the number of columns, I also don't think it will work because of the characters not being the same width.

I tried generating a string to be passed as the first argument of String.Format and then supply the array of characters for a row as the second argument, but I get the error Index (zero based) must be greater than or equal to zero and less than the size of the argument list. which is confusing since I have guaranteed this to be true.

//generatedText is built in a loop consisting of  += "{" + i + ", -2} "
row1.Text = String.Format(generatedText, myCharArray); //error!

The trivial solution of just separating each column by a space doesn't work at all because the characters aren't the same width so they quickly get offset (I think String.Format fixes this problem, if I could get it to work).

Any advice on fixing my usage of String.Format or a way to make the text fixed width perhaps, or another solution to my problem?

EDIT: I attempted to use String.Format for a determined amount of columns and it doesn't align them properly because of the different character widths. Other solutions?

Example output would be something like...

Row 1:  a  j  e  t  v  q  p  z  c
Row 2:  c  c  b  r  m  a  s  t  m

with the columns lining up perfectly.

There is no reliable way to do what you want unless you use a monospaced font as @Scott-Hunter recommended. You could fudge it by measuring strings, but with a non-monospaced font there are going to be gaps that you just can't fix using spaces.

With a monospaced font, it's trivial. Just separate with spaces. Use Consolas, Courier, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM