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

如何在移除ListBox中的项后扣减总计金额?

解决ListBox移除项时同步更新总计金额的问题

你的核心问题是移除lborder项时,原代码用txtprice的数值去减是错误的——这个文本框的内容和当前要移除的项的价格没有关联,正确的做法是从要移除的项本身提取对应的价格,再更新总计。下面给出两种可行方案:

方案一:用自定义类存储结构化数据(推荐)

这种方式更规范,后续维护和扩展更方便,不会依赖字符串格式。

第一步:定义菜单项目类

在你的命名空间下添加一个简单的类,用来存储项目名称和价格,并重写ToString方法保证ListBox显示效果不变:

namespace WinFormsApp1
{
    public class MenuItem
    {
        public string Name { get; set; }
        public double Price { get; set; }

        public override string ToString()
        {
            return $"{Name} = P{Price}";
        }
    }
}

第二步:修改添加项的代码

替换原button1_Click方法,确保存储的是结构化对象,同时增加格式校验避免异常:

private void button1_Click(object sender, EventArgs e)
{
    // 先判断是否选中了菜单项目
    if (lbmenu.SelectedItem == null)
        return;
    // 校验价格输入是否有效
    if (!double.TryParse(txtprice.Text, out double inputPrice))
    {
        MessageBox.Show("请输入有效的数字价格");
        txtprice.Focus();
        return;
    }

    // 创建结构化对象并添加到lborder
    var orderItem = new MenuItem
    {
        Name = lbmenu.SelectedItem.ToString(),
        Price = inputPrice
    };
    lborder.Items.Add(orderItem);

    // 更新总计金额(处理空值,默认从0开始)
    double currentTotal;
    double.TryParse(txttp.Text, out currentTotal);
    currentTotal += inputPrice;
    txttp.Text = currentTotal.ToString("F2"); // 保留两位小数,符合货币显示

    txtprice.Clear();
    txtprice.Focus();
}

第三步:修改移除项的代码

替换原button2_Click方法,从选中的结构化对象中提取价格并更新总计:

private void button2_Click(object sender, EventArgs e)
{
    // 判断是否选中了要移除的项
    if (lborder.SelectedIndex == -1)
        return;

    // 获取选中的结构化对象
    var selectedOrderItem = lborder.SelectedItem as MenuItem;
    if (selectedOrderItem != null)
    {
        // 更新总计金额
        double currentTotal;
        double.TryParse(txttp.Text, out currentTotal);
        currentTotal -= selectedOrderItem.Price;
        txttp.Text = currentTotal.ToString("F2");
    }

    // 移除选中项
    lborder.Items.RemoveAt(lborder.SelectedIndex);
    txtprice.Clear();
    txtprice.Focus();
}

方案二:从现有字符串中提取价格(快速临时方案)

如果不想修改数据存储方式,可以直接从lborder的字符串项中分割出价格,但这种方式依赖固定的字符串格式,一旦显示格式变动就会失效:

修改原button2_Click方法即可:

private void button2_Click(object sender, EventArgs e)
{
    if (lborder.SelectedIndex == -1)
        return;

    string itemText = lborder.SelectedItem.ToString();
    // 分割字符串提取价格部分(依赖"= P"的固定格式)
    int pricePos = itemText.IndexOf("= P") + 3;
    if (pricePos > 2 && double.TryParse(itemText.Substring(pricePos), out double itemPrice))
    {
        double currentTotal;
        double.TryParse(txttp.Text, out currentTotal);
        currentTotal -= itemPrice;
        txttp.Text = currentTotal.ToString("F2");
    }

    lborder.Items.RemoveAt(lborder.SelectedIndex);
    txtprice.Clear();
    txtprice.Focus();
}

额外优化建议

  • 用double.TryParse替代double.Parse,避免输入非数字文本时抛出异常;
  • 总计金额保留两位小数,更符合货币展示的规范;
  • 添加操作前的空值校验(比如是否选中菜单、价格是否有效),提升用户体验。

内容的提问来源于stack exchange,提问作者Joel Bruce

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 19:13:26