2013-07-25 36 views
1

使用此:對齊RichTextBox的

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

我得到這個:

enter image description here

我怎麼會完全將其調整到這一點:

enter image description here

+0

我建議使用gridview .. –

+0

我不認爲這是可能的,除非你使用像'Courier New'這樣的固定寬度的字體。 –

+0

刪除最後一行中的\ t並添加| –

回答

1

更換你的最後與

01代碼行
richTextBox1.AppendText("EmployeeName: " + "\t" + "Taborjakol"); 
1

如果你沒有使用一個RichTextBox,您應該definetly看看在GridView或者如果您使用第三方工具,如Telerik的,DevCraft,ComponentOne的和其他人大多都會有一個叫做屬性網格控件。有一個佈局,你可能會感興趣的

如果沒有其他方式使用RichTextBox的身邊,你必須做到以下幾點:

  1. 獲取一個固定寬度的字體,或者叫等寬字體http://en.wikipedia.org/wiki/Monospaced_font

  2. Evalute它們共享相同的寬度製表符的大寫金額(我不知道字符的數量都以自己與測試看着辦吧)

  3. 獲取文本的最大長度在左側(你的「COLUMNNAMES」我猜 - 像「EmployeeName」)

  4. 做一些數學 - 最大長度+一個標籤= X字符

  5. 現在在你的左邊與neccessary標籤填充剩餘的文本(可是從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; } 
} 
0

如前所述,你首先會想將字體設置爲單空字體,這意味着每個字母將是相同的大小。

所以,你會看到這樣的事情:

EEEEEEEEEEE

不是

EeEeeiiiiiiiiiiiiiiiiii

其次,你可以對齊文本這可能會提高它稍微:

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

但是,除非你有,我會使用不同的控制建議:

  • 的GridView

  • 列表框

這些都是很容易的格式,GridView控件具有選項卡,並且在輸入值時可以使用string.Format列表框。

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控件中每個選項卡的空格。您需要嘗試使用選項卡設置才能獲得文字的最佳效果。