0

guys, sorry to flood this website with such a boring question, but even after reading the documentation about C# and looking at their examples, I just can't seem to find an explanation for this.

Namely, I have a code like this:

 String s = "Hello"; String n = "World"; String s1 = "LALALALALALALA"; String n1 = "heyy"; String s2 = "sffsdfsfdsfsd"; String n2 = "dsfsdfdsfdsfsdsd"; String z = String.Format("{0,-20}{1,-10}", s, n); String z1 = String.Format("{0,-20}{1,-10}", s1, n1); String z2 = String.Format("{0,-20}{1,-10}", s2, n2); 

(This code is simply meant to be a test)

Now, when I use:

 Console.WriteLine(z); Console.WriteLine(z1); Console.WriteLine(z2); 

to write the output to the console, it works as I expected it to; 20 spaces are allocated to the string argument {0}, and the next spaces are allocated to the string argument {1}. However, when I run the exact same code in a form (as a MessageBox output, and even a TextBox text I get scrambled results. Argument {0} is okay, but then argument {1} starts at a seemingly random distance from the ending of {0}.

Is there a reason why this is happening? How can I fix it?

Thanks for any answers!

1 Answer 1

6

It depends on the font. Some fonts do not have the same width for all letters. If you try Consolas it will work fine.

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

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.