首页 > 解决方案 > 无法在我的 c# 程序中为产品添加数字

问题描述

我正在尝试制作购物清单并向其中添加不同的商品,但我不知道如何添加商品的价格并获得结果。

这是我正在使用的代码,它当前给了我这个错误,“输入字符串的格式不正确”

 public partial class Form1 : Form
{
    List<string> Items;
    public Form1()
    {
        InitializeComponent();
        Items = new List<string>();
    }

    private void BtnAdd_Click(object sender, EventArgs e)
    {
        double num;
        num = double.Parse(txtItems.Text);


        Items.Add(txtItems.Text);
        listBox1.Items.Clear();
        for(int i =0;i< Items.Count; i++)
        {
            listBox1.Items.Add(Items[i] + num);
        }

    }

标签: c#

解决方案


如果您需要关联两个值,例如名称和价格,您应该创建一个包含这两个值的类。如果您命名 this class Item,那么您可以将 aList<Item> Items = new List<Item>();与所有项目一起存储。

我建议您阅读有关类的更多信息以了解其工作原理。


推荐阅读