我该如何调整加入列表框项目字符串? [英] How can I align strings added as listbox items?
问题描述
我有一个列表框,我想从一个字符类显示姓名和高分列表。我使用 ListBox.Items.Add
添加每个字符以下字符串:
I have a listbox where I want to display a list of names and highscores from a character class. I use ListBox.Items.Add
to add the following string for each character:
public String stringHighscore()
{
return name + "\t\t\t" + score.ToString();
}
的问题是,当名称超过一定长度,比分被推在右边。列表框看起来像这样(对不起,我的代表处不允许我还没有发表图片):
The problem is that when the name exceeds a certain length, the score gets pushed to the right. The listbox looks like this (sorry, my rep doesn't allow me to post images yet):
我还以为这可能是由于\\ \\t,但我不知道。我怎样才能解决这个问题,并适当调整分数?它会更好,如果我用了两个列表框,一个名称,一个用于分数?
I have thought this may be due to the "\t" but I am not sure. How can I solve this and properly align the scores? Would it be better if I used two listboxes, one for names and one for scores?
推荐答案
您可以使用的 String.PadRight
方法。
You can use String.PadRight
method.
返回一个新的字符串由
填充他们左对齐此字符串中的字符用空格右边,在指定总长度。
Returns a new string that left-aligns the characters in this string by padding them with spaces on the right, for a specified total length.
让我们假设你有20个字符长度名称
作为最大
Let's say you have 20 characters length for name
as maximum
public String stringHighscore()
{
return name + name.PadRight(20 - name.Length) + "\t\t\t" + score.ToString();
}
如果你的名字的长度 13
,这将增加 7
空格字符。而这样一来,你的所有名称的长度将等于( 20
)的结尾。
If your name's length is 13
, this will add 7
space characters. And that way, your all name's length will equal (20
) at the end.
这篇关于我该如何调整加入列表框项目字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!