You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.NET点击Pay按钮如何向ListView添加多商品项、数量、价格及总计数据

问题描述

当顾客购买多件商品时,需要将商品项、购买数量、商品单价、总计金额信息添加到ListView控件中。当前点击「Pay」按钮后,ListView中仅能添加Milk商品的对应行,无法展示顾客购买的其他商品。

参考界面截图:
POS系统ListView参考界面

原有实现代码:

Public Class Form1

    Private Sub list_column()

        With ListView1
            .View = View.Details
            .FullRowSelect = True
            .GridLines = True

            .Columns.Add("Customer")
            .Columns.Add("Item")
            .Columns.Add("Quantity")
            .Columns.Add("Price")
            .Columns.Add("Total")
            .Columns.Add("Amount Tendered")
            .Columns.Add("Amount Changed")

        End With

    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Call list_column()

    End Sub

 Private Sub btn_Pay_Click(sender As Object, e As EventArgs) Handles btn_Pay.Click

        Dim Milk As Integer
        Dim Coffee As Integer
        Dim Sugar As Integer
        Dim Coke As Integer
        Dim Cotton As Integer

        ' Price of the product
        Milk = 10
        Coffee = 30
        Sugar = 20
        Coke = 3
        Cotton = 4

        Dim item As Integer
        item = ListView1.Items.Count
        Dim list As New ListViewItem(txt_CustomerName.Text)

        'Milk
        list.SubItems.Add("Milk")
        list.SubItems.Add(txt_Milk.Text)
        list.SubItems.Add(Milk)
        list.SubItems.Add(txt_Total.Text)
        list.SubItems.Add(txt_AmountTendered.Text)
        list.SubItems.Add(txt_AmountChanged.Text)
        ListView1.Items.Add(list)

    End Sub
End Class
问题原因

代码仅创建了1个ListViewItem实例,且硬编码写入了Milk的商品信息,未对其余商品做数量判断、也未为其余商品创建独立行对象添加到控件,因此仅能显示Milk单条记录。

修复代码

替换原有btn_Pay_Click事件逻辑即可:

Private Sub btn_Pay_Click(sender As Object, e As EventArgs) Handles btn_Pay.Click
    ' 配置商品单价、对应数量输入框映射,避免重复冗余代码
    Dim productPriceMap As New Dictionary(Of String, Integer) From {
        {"Milk", 10},
        {"Coffee", 30},
        {"Sugar", 20},
        {"Coke", 3},
        {"Cotton", 4}
    }
    Dim productQtyTxtMap As New Dictionary(Of String, TextBox) From {
        {"Milk", txt_Milk},
        {"Coffee", txt_Coffee},
        {"Sugar", txt_Sugar},
        {"Coke", txt_Coke},
        {"Cotton", txt_Cotton}
    }

    Dim isFirstRow As Boolean = True
    Dim customerName As String = txt_CustomerName.Text.Trim()
    Dim totalPaid As String = txt_AmountTendered.Text.Trim()
    Dim change As String = txt_AmountChanged.Text.Trim()

    ' 遍历所有商品,仅添加购买数量大于0的有效商品行
    For Each product In productPriceMap
        Dim buyQty As Integer
        ' 跳过数量输入无效、购买数量为0的商品
        If Not Integer.TryParse(productQtyTxtMap(product.Key).Text.Trim(), buyQty) OrElse buyQty <= 0 Then
            Continue For
        End If

        Dim newRow As New ListViewItem()
        If isFirstRow Then
            ' 第一行填充整单公共信息
            newRow.Text = customerName
            newRow.SubItems.Add(product.Key)
            newRow.SubItems.Add(buyQty.ToString())
            newRow.SubItems.Add(product.Value.ToString())
            newRow.SubItems.Add((buyQty * product.Value).ToString())
            newRow.SubItems.Add(totalPaid)
            newRow.SubItems.Add(change)
            isFirstRow = False
        Else
            ' 其余商品行公共列留空,仅填充商品自身信息
            newRow.Text = String.Empty
            newRow.SubItems.Add(product.Key)
            newRow.SubItems.Add(buyQty.ToString())
            newRow.SubItems.Add(product.Value.ToString())
            newRow.SubItems.Add((buyQty * product.Value).ToString())
            newRow.SubItems.Add(String.Empty)
            newRow.SubItems.Add(String.Empty)
        End If
        ListView1.Items.Add(newRow)
    Next
End Sub
实现说明
  • ListView每一行对应一个独立的ListViewItem实例,禁止复用同一个实例添加多行数据
  • 遍历所有商品时先做数量校验,避免添加数量为0、输入非法的无效行
  • 整单维度的客户名、实付金额、找零金额仅在第一行填充,其余商品行对应列留空,和参考界面展示逻辑一致
  • 单商品行的Total字段按「购买数量*商品单价」计算单行小计,不要直接用整单总金额填充

内容的提问来源于stack exchange,提问作者Mark Xuckerberg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.29 08:36:17