2

使用这个:

richTextBox1.AppendText("EMPID: " + "\t\t" + "4001");
richTextBox1.AppendText(System.Environment.NewLine);
richTextBox1.AppendText(System.Environment.NewLine);
richTextBox1.AppendText("EmployeeName: " + "\t\t" + "Taborjakol");

我懂了:

在此处输入图像描述

我将如何完美地将其与此对齐:

在此处输入图像描述

4

4 回答 4

5

您可以做的是更改 RichTextBoxControl 的选项卡位置。

richTextBox1.SelectionTabs = new int[] { 90, 180, 270, 360 };

richTextBox1.AppendText("EMPID: " + "\t\t" + "4001");
richTextBox1.AppendText(System.Environment.NewLine);
richTextBox1.AppendText(System.Environment.NewLine);
richTextBox1.AppendText("EmployeeName: " + "\t\t" + "Taborjakol");

SelectionsTab 属性重新定义用于 RichTextBox 控件中每个选项卡的空格。您需要尝试选项卡设置以获得最佳文本效果。

于 2013-07-25T07:55:30.630 回答
1

将最后一行代码替换为

richTextBox1.AppendText("EmployeeName: " + "\t" + "Taborjakol");
于 2013-07-25T07:28:49.523 回答
1

如果你不必使用richtextbox,你应该看看gridview,或者如果你使用Telerik、DevCraft、ComponentOne等第三方工具,其中大多数都会有一个名为Property Grid的控件,它的布局可能是有兴趣。

如果在使用富文本框时没有其他方法,您必须执行以下操作:

  1. 获取固定宽度字体或称为等宽字体http://en.wikipedia.org/wiki/Monospaced_font

  2. 评估与制表符共享相同宽度的字符数量(我不知道字符的数量必须通过测试自己弄清楚)

  3. 在左侧获取文本的最大长度(我猜你的“列名” - 比如“EmployeeName”)

  4. 做一些数学运算 - 最大长度 + 一个制表符 = x 个字符

  5. 现在用必要的选项卡(可以是从 1 到 x 的任何值)填充左侧的剩余文本,以获得与 4 中计算的相同数量的字符。

但是对于这种情况,richtextbox 也不是理想的控件。

编辑:

这里有一些代码:

public partial class Form1 : Form
{
    private const int FetchTestData = 50;
    private const int TabCharLength = 5;

    public Form1()
    {
        InitializeComponent();

        //With this Fontsettings - 5 chars = 1 Tab - this changes with different fonts
        this.richTextBox1.Font = new System.Drawing.Font("Courier New", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

        var type = typeof(TestData);
        var list = GetTestData();
        var maxProperty = GetMaxProperty(type);
        maxProperty = FillToNext(maxProperty);
        var properties = GetProperties(type);

        for (var i = 0; i < FetchTestData; i++)
        {
            var data = list[i];

            foreach (var propertyInfo in properties)
            {
                richTextBox1.AppendText(propertyInfo.Name);

                var tabs = GetNumberOfTabs(maxProperty, propertyInfo.Name.Length);
                for (var j = 0; j < tabs; j++)
                    richTextBox1.AppendText("\t");

                richTextBox1.AppendText(Convert.ToString(propertyInfo.GetValue(data)));

                richTextBox1.AppendText(Environment.NewLine);
            }

            if (i >= FetchTestData - 1) 
                continue;

            richTextBox1.AppendText(Environment.NewLine);
            richTextBox1.AppendText("---------- NEXT DATA ----------");
            richTextBox1.AppendText(Environment.NewLine);
        }
    }

    private int GetNumberOfTabs(int maxLength, int textLength)
    {
        if ((maxLength % TabCharLength) != 0)
            maxLength = FillToNext(maxLength);

        var difLength = maxLength - textLength;

        return (int)(Math.Floor(Convert.ToDouble(difLength / TabCharLength)) + 1);
    }

    private int FillToNext(int maxLength)
    {
        return maxLength + (5 - (maxLength % TabCharLength));
    }

    private PropertyInfo[] GetProperties(Type type)
    {
        if (type == null)
            throw new ArgumentNullException("type");

        return type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
    }

    private int GetMaxProperty(Type type)
    {
        if (type == null)
            throw new ArgumentNullException("type");

        return (from x in GetProperties(type)
                select x.Name.Length).Max();
    }

    private List<TestData> GetTestData()
    {
        var returnValue = new List<TestData>();

        for (int i = 0; i < FetchTestData; i++)
        {
            returnValue.Add(new TestData()
            {
                ID = i,
                Name = "NameValue " + i,
                Description = "DescriptionValue " + i,
                PropertyA = "PropertyAValue " + i,
                PropertyB = "PropertyBValue " + i,
                SomeReallyLongPropertyName = "RandomStuff... " + i
            });
        }

        return returnValue;
    }
}

public class TestData
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string PropertyA { get; set; }
    public string PropertyB { get; set; }
    public string SomeReallyLongPropertyName { get; set; }
}
于 2013-07-25T07:45:14.593 回答
0

如前所述,您首先要将字体设置为等宽字体,这意味着每个字母的大小相同。

所以你会看到这样的东西:

EEEEEEEEEE

而不是

啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊

其次,您可以对齐文本,这可能会稍微改善它:

richTextBox1.SelectAll();
richTextBox1.SelectionAlignment = HorizontalAlignment.Center;

但是,除非您必须这样做,否则我建议您使用不同的控件:

  • 网格视图

  • 列表框

这些都易于格式化,gridview 有选项卡,并且您可以在输入值时使用 string.Format 的列表框。

于 2013-07-25T07:49:29.033 回答