vendredi 17 avril 2015

Specify max width for a string in c# [duplicate]


This question already has an answer here:




So I have to manage a listbox that has columns like table and I would like to give each column a fixed width. I wrote this method to convert the strings into a fixed length:



const int nameMaxWidth = 20;
int nameLength = name.Length;

if (nameLength < 20)
{
while (nameLength < 20)
{
name = name + " ";
nameLength++;
}
}
else
{
name = name.Substring(0, nameMaxWidth);
}


but it seems that it does not work since each letter has different size, for example nnnnn is longer than iiiii although both have the same length. I have tried String.Format but it doesnt work either. Is there anything I can do?


Aucun commentaire:

Enregistrer un commentaire