Windows Forms TextBox Bug?

Ser*_*iev 4 c# textbox winforms

我有Windows Forms项目.表单上有一个textBox,名为txt.任务是在两列中的textBox解析文本中编写用户的字符串.每列必须具有左侧对齐.这是一个例子:

--------------------------
Parameters          Values

height              36
width               72
length of trousers  32
--------------------------
Run Code Online (Sandbox Code Playgroud)

每个值都必须在另一个之下.显然,我们需要一个方法,在每个参数之后输入必要数量的空格.我开发了这个方法:

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();
}
Run Code Online (Sandbox Code Playgroud)

我使用以下字符串测试了此方法:

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;
Run Code Online (Sandbox Code Playgroud)

制作完成后

txt.Text += report;
Run Code Online (Sandbox Code Playgroud)

我有一个意外的图片:

TextBox输出

之后我尝试在文件中编写测试字符串.这是结果:

文件输出

文件中的输出是正确的.文本框中的输出是错误的.textBox出了点问题.如何解决这个问题?这是我的测试项目的代码:

/*
 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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Lar*_*ech 9

尝试将TextBox控件的Font更改为Mono-Spaced字体,例如Courier New.

看起来你的TextBox仍然使用默认字体Microsoft Sans Serif,每个字母的宽度都不同.