Windows Forms TextBox Bug?

c#, textbox, winforms

Solution

Try changing your TextBox control's Font to a Mono-Spaced font, like `Courier New`.

It looks like your TextBox is still using the default font `Microsoft Sans Serif`, which will have different widths for each letter.

Problem

I've got Windows Forms project. There's a textBox on the form, called txt. The task is to write user's string in the textBox parsing text in two columns. Each column must have left-side alignment. Here's the example: ``` -------------------------- Parameters Values height 36 width 72 length of trousers 32 -------------------------- ``` Each value has to stand one under another. Obviously, we need a method, that inputs the necessary number of spaces after each parameter. I've developed this method: ``` private string AddSpaces(string str) { const int MAX_WIDTH = 50; // We've got a 50 symbols field to fill it with current parameter // name and add necessary number of spaces. StringBuilder strWithSpaces = new StringBuilder(); int numOfSpaces = MAX_WIDTH - str.Length; for (int i = 0; i < numOfSpaces; i++) { strWithSpaces.Append(" "); } return strWithSpaces.ToString(); } ``` I have tested this method using following string: ``` string report = Environment.NewLine + "-------------" + DateTime.Now + "-------------" + Environment.NewLine + "Вихідні дані:" + Environment.NewLine + "a:" + AddSpaces("a:") + "1" + Environment.NewLine + "ab:" + AddSpaces("ab:") + "1" + Environment.NewLine + "abcdefg:"+ AddSpaces("abcdefg:") + "1" + Environment.NewLine; ``` And after making ``` txt.Text += report; ``` I have an unexpected picture: TextBox Output After that I've tried to write the test string in file. Here's the result: File Output The ouput in file is correct. The output in textbox is wrong. Something's wrong with textBox. How to resolve this problem? Here's the code of my test project: ``` /* Correct output looks like this: a: 1 ab: 1 abcdefg: 1 */ using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; namespace spaces { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { string report = Environment.NewLine + "-------------" + DateTime.Now + "-------------" + Environment.NewLine + "Вихідні дані:" + Environment.NewLine + "a:" + AddSpaces("a:") + "1" + Environment.NewLine + "ab:" + AddSpaces("ab:") + "1" + Environment.NewLine + "abcdefg:" + AddSpaces("abcdefg:") + "1" + Environment.NewLine; txt.Text += report; using (StreamWriter outfile = new StreamWriter(@"D:\test.txt")) { outfile.Write(report); } } private string AddSpaces(string str) { const int MAX_WIDTH = 50; StringBuilder strWithSpaces = new StringBuilder(); int numOfSpaces = MAX_WIDTH - str.Length; for (int i = 0; i < numOfSpaces; i++) { strWithSpaces.Append(" "); } return strWithSpaces.ToString(); } } } ```

Original source