Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aligining Strings In WPF

I want to print data like this:

http://www.csharp-examples.net/align-string-with-spaces/


 -------------------------------
 Bi1l       | Gates      |    51
 EdnA       | Parker     |   114
 JohNny     | Depp       |    44
 -------------------------------

I tried using the example above, PadRight and PadLeft and more calculations of length, and I always got this type of output (my output goes to a TextBox)


 -------------------------------
 Bill      | Gates      |    51
 Edna       | Parker  |   114
 Johnny       | Depp       |    44
 -------------------------------

For what I think, it happens because WPF doesn't give a fixed and equal block for every character, so W takes more space than i, and makes the separations look bad.

Can someone help me out with a solution?

like image 462
user3731023 Avatar asked May 11 '26 11:05

user3731023


1 Answers

You can change your font type to Monospace font (Wikipedia) like Courier.

According to the linked wikipedia article other possible font types are Courier New, Lucida Console, Monacoor Consolas.


Alternatively you can change your UI and display your data using some kind of grid control like the DataGrid control to display your data.

like image 155
Martin Avatar answered May 14 '26 01:05

Martin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!