我在c#中使用了RichTextBox。我需要在一个RichTextbox内显示不同长度的字符串,但这些字符串应该完美对齐..这是一个例子..

abcd   abcde  abcde
ab     abc    abcdef
.

我知道如何使用setw函数在c ++中执行此操作..但我在C#中找不到等效物。

有帮助吗?

解决方案

可以使用String.PadRight

innerString.PadRight(10);
.

其他提示

string varName=String.Format("{0,10:D}", 2);
.

将数字2格式化为宽度10的绳子串,使用-5以5的宽度对齐5 ...

来源: http://answers.yahoo.com/question/index?qid=20100727164827AAQJ1HN

我为此目的创建了一个函数:

public string tab(string s, int w)
{
    //w is the desired width
    int stringwidth = s.Length;
    int i;
    string resultstring = s;

    for(i=0;i<=(w-stringwidth)/8;i++)
    {
        resultstring = resultstring + "\t";
    }
    return resultstring;
 }
. 然后,将其添加到列表框中,例如:

listBox.Items.Add(tab("MyFullNameHere",30)+ tab("MyContact - xxxxx",12));
listBox.Items.Add(tab("MyWifeFullNameHereVeryLong", 30) + tab("HerContact - xxxxx", 12));
.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top