Sergey Shafiev
Sergey Shafiev

Reputation: 4375

Windows Forms TextBox Bug?

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();
        }
    }
}

Upvotes: 4

Views: 1206

Answers (3)

aybe
aybe

Reputation: 16692

You need to set the font of the TextBox to a Monospaced font, it will look like as you want it then.

Upvotes: 3

Fredrik M&#246;rk
Fredrik M&#246;rk

Reputation: 158399

Since you are not using a monospace font, spaces and characters have different widths. Spaces are typically quite narrow in proportional fonts, so if you have more spaces, the line will seem shorter than a main with fewer spaces. Change to a monospace font (such as Courier or Consolas) and it will look better.

Upvotes: 4

LarsTech
LarsTech

Reputation: 81675

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.

Upvotes: 9

Related Questions