如何从ListBox字符串项中提取整数计算并排除无关文本?
解决ListBox选中项提取整数金额求和的问题
我明白你的问题啦——现在你把服务名称和带货币格式的金额拼成字符串放进ListBox里,点击按钮时直接拿这些字符串去相加肯定不行,因为它们是文本不是数字。下面给你两种解决思路,从快速修改现有代码到更规范的最佳实践都有:
方法一:从现有字符串中提取整数金额
既然你已经把字符串加到ListBox里了,咱们可以通过字符串解析把数字部分提取出来,再转成整数计算。这里用正则表达式来匹配字符串里的连续数字,不管货币符号在什么位置都能准确抓到:
修改你的CalcButton_Click事件代码如下:
Private Sub CalcButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CalcButton.Click ' 处理外部服务选中项 Dim exteriorText As String = ExteriorListBox.SelectedItem.ToString() ' 用正则匹配所有连续数字 Dim exteriorMatch = System.Text.RegularExpressions.Regex.Match(exteriorText, "\d+") ' 成功匹配就转成整数,否则默认0 Dim exteriorAmount As Integer = If(exteriorMatch.Success, Integer.Parse(exteriorMatch.Value), 0) ' 处理内部服务选中项 Dim interiorText As String = InteriorListBox.SelectedItem.ToString() Dim interiorMatch = System.Text.RegularExpressions.Regex.Match(interiorText, "\d+") Dim interiorAmount As Integer = If(interiorMatch.Success, Integer.Parse(interiorMatch.Value), 0) ' 显示单个金额和总和 ExteriorLabel.Text = Format(exteriorAmount, "currency") InteriorLabel.Text = Format(interiorAmount, "currency") TotalLabel.Text = Format(exteriorAmount + interiorAmount, "currency") End Sub
方法二:用自定义类存储服务信息(更规范的最佳实践)
如果以后还要维护或扩展这个功能,直接存字符串的方式会越来越麻烦。咱们可以创建一个专门的类来存储服务的名称和价格,把对象加到ListBox里,这样需要价格时直接从对象里取,完全不用解析字符串:
第一步:定义Service类
Public Class Service ' 服务名称 Public Property Name As String ' 服务价格(整数) Public Property Price As Integer ' 重写ToString方法,让ListBox显示咱们想要的文本格式 Public Overrides Function ToString() As String Return $"{Name} {Format(Price, "currency")}" End Function End Class
第二步:修改添加ListBox项的代码
把原来的字符串添加改成添加Service对象:
' 添加外部服务 ExteriorListBox.Items.Add(New Service With {.Name = "Wash and Go", .Price = 35}) ExteriorListBox.Items.Add(New Service With {.Name = "Wash and Dry", .Price = 60}) ExteriorListBox.Items.Add(New Service With {.Name = "Wash, Dry and Tyres", .Price = 70}) ExteriorListBox.Items.Add(New Service With {.Name = "Wash, Dry, Tyres and Polish", .Price = 150}) ' 添加内部服务 InteriorListBox.Items.Add(New Service With {.Name = "No Work", .Price = 0}) InteriorListBox.Items.Add(New Service With {.Name = "Vacuum", .Price = 20}) InteriorListBox.Items.Add(New Service With {.Name = "Valet", .Price = 200})
第三步:修改按钮点击事件
直接从选中的Service对象里获取价格计算:
Private Sub CalcButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CalcButton.Click ' 把选中项转成Service对象 Dim exteriorService As Service = DirectCast(ExteriorListBox.SelectedItem, Service) Dim interiorService As Service = DirectCast(InteriorListBox.SelectedItem, Service) ' 确保选中项不为空,避免空引用错误 If exteriorService IsNot Nothing AndAlso interiorService IsNot Nothing Then Dim exteriorAmount = exteriorService.Price Dim interiorAmount = interiorService.Price ExteriorLabel.Text = Format(exteriorAmount, "currency") InteriorLabel.Text = Format(interiorAmount, "currency") TotalLabel.Text = Format(exteriorAmount + interiorAmount, "currency") End If End Sub
这种方法的好处是代码更清晰,以后要加新的服务属性(比如服务时长、折扣)都非常方便,也不会出现字符串解析出错的情况。
内容的提问来源于stack exchange,提问作者W.Gwala
相关产品推荐
相关产品推荐

