【发布时间】:2019-01-27 17:16:27
【问题描述】:
当我从表单左侧单击我的产品时,我会在 ListBox 填充名称和价格上看到它们。
但是因为每个产品名称的名称长度都不同,所以价格是丑陋的格式,这不是你在我的图片左侧看到的直接方式。
如果您在我的附加图片上看到它们,我希望它们像右侧一样。
我在列表框格式事件中的代码如下,但无法正常工作。请帮忙!
private void ListOfPaidProducts_Format(object sender, ListControlConvertEventArgs e)
{
string currentProductName = ((Product)e.ListItem).Description;
string currentPrice = String.Format("{0}", ((Product)e.ListItem).Price); // I tried even with {0,20}....
string currentProductNamePadded = currentProductName.PadRight(30);
e.Value = currentProductNamePadded + currentPrice + "$";
}
【问题讨论】:
-
您可能想改用
DataGridView。虽然我建议您使用DataGridView,但仅供参考,如果不使用等距字体,您将无法为ListBox实现干净整洁的多列格式,除非您决定进行自定义绘画,例如like this。 -
您可以使用
ListBox的UseCustomTabOffsets 功能,或者更好地使用ListView 或DataGridView,如前所述。由于文本长度可能会发生变化,您可能希望避免硬编码值或不断测量文本,因此不同的控件是显而易见的选择。
标签: c# winforms listbox formatting