代码之家  ›  专栏  ›  技术社区  ›  kaiselwyvrn

我需要显示来自用户输入的条形码的相应数据

  •  0
  • kaiselwyvrn  · 技术社区  · 1 年前

    这个程序很简单,但我似乎听不懂它>&书信电报;

    因此,用户必须在第一个文本框中写一个条形码,在第二个文本框上写产品,然后在单击添加按钮后将这两个条形码保存到数组列表中。然后下一个部分是搜索栏,用户在其中搜索条形码,并且该搜索框下方的标签应该显示条形码的相应产品。

    我认为我的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 回复  |  直到 1 年前
        1
  •  0
  •   oerkelens    1 年前

    您试图在其他无序列表的基础上从一个无序列表中检索项目。您可以使用索引来实现这一点,但这是笨拙、复杂和容易出错的。

    我建议使用 Dictionary ,基于存储项目 key ,因此您可以轻松地检索它们。

    var productDictionary = new Dictionary<string, string>();
    

    添加到其中:

    productDictionary[txt_barcode.Text] = txt_product.Text;
    

    并检索它:

    if (productDictionary.ContainsKey(txt_search.Text)
    {
        lbl_product.Text = productDictionary[txt_search.Text];
    }
    

    不需要在多个列表上循环,而且还可以避免使用双键的潜在问题。

    注意,我还删除了对的不必要转换 int 。如果您收到一个字符串,为什么不将其存储为字符串?如果它需要是一个整数,您应该 验证 它是一个整数,而不是盲目地转换它。如果你试图将“123AB”或“Foo”转换为 int ,这将如何影响您的搜索结果?;)

        2
  •  0
  •   CodeJunkie    1 年前

    首先,在对数据进行任何操作之前,您总是需要验证用户输入。

    int barcode = Convert.ToInt32(txt_barcode.Text);
    

    如果用户在您的 TextBox
    你可以这样做:

    int barcode;
    if(!Int.TryParse(txt_barcode.Text, out barcode))
    {
       MessageBox.Show("Please enter a valid barcode!");
       return;
    }
    

    请记住,条形码值并不总是数字的,因此现实世界中的场景很可能会要求字符串值。

    我认为没有充分的理由保留 Product Barcode 值分离。创建一个简单的类用作您的模型,如下所示:

    public class Product
    {
        string barcode;
        public string Barcode
        {
            get => barcode;
            set => barcode = value;
        }
    
        string product_name;
        public string ProductName
        {
            get => product_name;
            set => product_name = value;
        }
    }
    

    然后当你填写 Collection 你可以这样做:

    // This is the collection you will be storing your products in
    List<Product> Products = new List<Product>();
    private void btn_add_Click(object sender, EventArgs e)
    {
        string barcode = txt_barcode.Text;
        string product = txt_product.Text;
    
        Product myProduct = new Product()
        {
            Barcode = barcode,
            ProductName = product
        };
    
        Products.Add(Product); // MyCollection is of List<Product> type
    }
    

    然后你可以通过以下产品列表来搜索整个内容:

    private Product SearchForProduct()
    {
        foreach (Product p in Products)
        {
             // This will return the first product that matches either of the cases
            // Play around with you search logic
            if (p.Barcode == "YourSearchTerm" || p.ProductName == "YourSearchTerm")
            {
                return p; // Returns the product that matches the search terms
            }
        }
    
        return null; // Returns null if no product matching the search term is found
    }
    

    然后,您可以这样调用该方法:

    Product search_result = SearchForProduct(search_textbox.Text);
    if(search_result == null)
    {
      MessageBox.Show("No Product matching the search terms was found!");
      return;
    }
    else
    {
      // Do something with the found product 
    }