Question

J'utilise une RichTextbox dans C #.J'ai besoin d'afficher des chaînes de longueur différente à l'intérieur d'une richtextbox, mais ces chaînes doivent être parfaitement alignées. Ceci est un exemple ..

abcd   abcde  abcde
ab     abc    abcdef

Je sais comment faire cela en C ++ à l'aide de la fonction SETW .. Mais je n'ai pas pu trouver d'équivalent en C #.

Était-ce utile?

La solution

Vous pouvez utiliser String.PadRight

innerString.PadRight(10);

Autres conseils

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

Ceci formatera Numéro 2 sous forme de chaîne de largeur 10 à droite alignée, utilisez -5 pour l'avoir aligné à gauche dans la largeur de 5 ...

Source: http://answers.yahoo.com/question/index?qid=20100727164827AAQJJ1HN

J'ai créé une fonction à cet effet:

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

Ensuite, en ajoutant à une liste de liste, par exemple:

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top