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

求助:条码搜索对应产品功能异常,代码逻辑排查

问题描述

开发功能需求:用户在第一个文本框输入条码,第二个文本框输入产品名称,点击添加按钮后将二者分别存入barcodeList和productList两个ArrayList;在搜索栏输入条码时,下方标签应显示对应的产品名称。目前功能异常,仅能显示产品文本框的现有数据,怀疑foreach语句存在问题,相关代码如下:

private void btn_add_Click(object sender, EventArgs e)
{
    int barcode = Convert.ToInt32(txt_barcode.Text);
    barcodeList.Add(barcode);

    string product = txt_product.Text;
    productList.Add(product);

    MessageBox.Show("saved");
}

private void txt_search_TextChanged(object sender, EventArgs e)
{
    try
    {
        int barcodeSearch = Convert.ToInt32(txt_search.Text);

            for (int count = 0; count < barcodeList.Count; count++)
            {
                foreach (int barcode in barcodeList)
                {
                    if (barcodeSearch == barcode)
                    {
                        foreach (string product in productList)
                        {
                            lbl_product.Text = "" + product;
                        }
                    }
                }
            }   
    } catch (Exception ex)
    {
        
    }
}
问题排查与修复

核心问题

  1. 冗余循环+逻辑错误:
    • 外层for循环和内层foreach遍历barcodeList属于重复操作,完全没必要。
    • 找到匹配条码后,遍历整个productList逐个覆盖标签文本,最终只会显示列表最后一个产品,而非对应条码的产品。
  2. 未利用索引对应关系:条码和产品是一一添加的,两个列表的索引位置完全对应,但原代码没有通过索引关联两者。

修复方案

方案1:基于现有ArrayList结构修复

修改txt_search_TextChanged方法,去掉冗余循环,通过索引匹配对应产品:

private void txt_search_TextChanged(object sender, EventArgs e)
{
    try
    {
        int barcodeSearch = Convert.ToInt32(txt_search.Text);
        // 遍历条码列表找匹配项的索引
        for (int i = 0; i < barcodeList.Count; i++)
        {
            if ((int)barcodeList[i] == barcodeSearch)
            {
                // 用相同索引取对应产品
                lbl_product.Text = productList[i].ToString();
                return; // 找到后直接退出,避免无效遍历
            }
        }
        // 无匹配时的提示
        lbl_product.Text = "未找到对应产品";
    }
    catch (Exception ex)
    {
        // 输入非数字时的提示
        lbl_product.Text = "请输入有效条码";
    }
}

方案2:使用字典替代两个列表(更优解)

用Dictionary<int, string>存储条码与产品的键值对,从根源避免索引不匹配风险,逻辑更清晰:

首先替换原有两个ArrayList为字典:

private Dictionary<int, string> productDict = new Dictionary<int, string>();

修改添加按钮事件:

private void btn_add_Click(object sender, EventArgs e)
{
    try
    {
        int barcode = Convert.ToInt32(txt_barcode.Text);
        string product = txt_product.Text;
        
        // 避免重复添加相同条码
        if (!productDict.ContainsKey(barcode))
        {
            productDict.Add(barcode, product);
            MessageBox.Show("保存成功");
        }
        else
        {
            MessageBox.Show("该条码已存在");
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("请输入有效条码");
    }
}

修改搜索事件:

private void txt_search_TextChanged(object sender, EventArgs e)
{
    try
    {
        int barcodeSearch = Convert.ToInt32(txt_search.Text);
        if (productDict.ContainsKey(barcodeSearch))
        {
            lbl_product.Text = productDict[barcodeSearch];
        }
        else
        {
            lbl_product.Text = "未找到对应产品";
        }
    }
    catch (Exception ex)
    {
        lbl_product.Text = "请输入有效条码";
    }
}

额外优化建议

  • 添加输入验证:点击添加按钮时,检查条码是否为有效数字、产品名称是否为空,提升用户体验。
  • 完善异常处理:原代码catch块为空,建议添加提示信息,方便排查问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 04:32:24