【问题标题】:Concatenation of variable length strings with proper spacing以适当间距连接可变长度字符串
【发布时间】:2021-05-05 00:45:56
【问题描述】:

所以我所做的是在变量之间手动插入空格。虽然它有效,但如果变量有点长,它会向右移动。这是我完成的编码。

lstOutput.Items.Add("Items                                         Quantity                                         Prices")
lstOutput.Items.Add("Pizza Slices                                   " & quanPizza & "                                             " & FormatCurrency(pricePizza, 2))
lstOutput.Items.Add("Fries                                               " & quanFries & "                                             " & FormatCurrency(priceFries, 2))
lstOutput.Items.Add("Soft Drinks                                    " & quanSoftDrinks & "                                             " & FormatCurrency(priceSoftDrinks, 2))
lstOutput.Items.Add("")
lstOutput.Items.Add("Total                                                                                                " & FormatCurrency(totalPrice, 2))

这是变量有点长的时候。

我怎样才能让它像在一个列中一样?

【问题讨论】:

  • 您会考虑使用 ListView 还是 DataGridView?
  • 在使用比例字体的列表框中对齐文本的唯一方法是使用ListBox.CustomTabOffsets 设置物理标签位置(考虑到平均长度 的部分,你可以看到一个例子here)。你可能不想要那个。按照建议,使用 ListView:没有计算,没有奇怪的填充,没有麻烦。

标签: vb.net


【解决方案1】:

您可以使用Padding 按预期格式化您的字符串。像下面这样使用:

lstOutput.Items.Add("Items".PadRight(30) & "Quantity".PadRight(30) & "Prices".PadRight(30))
lstOutput.Items.Add("Pizza Slices".PadRight(30) & quanPizza.PadRight(30) & FormatCurrency(pricePizza, 2).ToString().PadRight(30))

这是给你的Example

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-20
    • 1970-01-01
    • 2012-02-24
    • 1970-01-01
    • 1970-01-01
    • 2021-11-02
    • 2013-10-09
    • 2018-07-01
    相关资源
    最近更新 更多