C#如何在我的列表框中计算我的价格

时间:2017-11-15 05:51:18

标签: c# database listbox

我是初学者,并试图为我们未来的业务创建一个简单的库存:)

我该怎么办这个问题?

当我点击列表框中的每个按钮时,我想计算价格。

示例:

当我添加商品且价格为6时,他会显示在底部(总计)6,当我添加另一个且价格为7时会添加到6所以13我可以使用foreach或其他什么来修改我的代码?帮助伙计们谢谢你们:)

enter image description here

private void button4_Click(object sender, EventArgs e)
{
    int sum = 0;
    if (listBox1.Items.Count >= 0)
    {
        listBox1.Items.Add(dataGridView1.CurrentRow.Cells[0].Value.ToString() + "                 " + dataGridView1.CurrentRow.Cells[1].Value.ToString() + "                                                      " + Convert.ToInt32(dataGridView1.CurrentRow.Cells[2].Value.ToString()) + "                        " + Convert.ToInt32(dataGridView1.CurrentRow.Cells[3].Value.ToString()));
        sum += Convert.ToInt32(dataGridView1.CurrentRow.Cells[3].Value.ToString());

    }
    label7.Text = sum.ToString();
}

当我取消交易清单

时,我无法扣除
private void button6_Click(object sender, EventArgs e)
{
    if (listBox1.SelectedIndex != -1)
    {
        listBox1.Items.RemoveAt(listBox1.SelectedIndex);
        sum -= Convert.ToInt32(dataGridView1.CurrentRow.Cells[3].Value.ToString());
    } 
}

4 个答案:

答案 0 :(得分:2)

您在按钮点击事件中使用int sum = 0;。因此,每次单击按钮时,Sum将被设置为默认值0。

int sum = 0;设置为全局声明,然后尝试。

如下所示:

int sum = 0;
private void button4_Click(object sender, EventArgs e)
    {

        if (listBox1.Items.Count >= 0)
        {
            listBox1.Items.Add(dataGridView1.CurrentRow.Cells[0].Value.ToString() + "                 " + dataGridView1.CurrentRow.Cells[1].Value.ToString() + "                                                      " + Convert.ToInt32(dataGridView1.CurrentRow.Cells[2].Value.ToString()) + "                        " + Convert.ToInt32(dataGridView1.CurrentRow.Cells[3].Value.ToString()));
            sum += Convert.ToInt32(dataGridView1.CurrentRow.Cells[3].Value.ToString());

        }
        label7.Text = sum.ToString();
    }

答案 1 :(得分:0)

使用LINQ的另一种方法。

using System.Linq; //import this namespace
//...
//...

private void button4_Click(object sender, EventArgs e)
{
    if (listBox1.Items.Count >= 0)
    {
        listBox1.Items.Add(dataGridView1.CurrentRow.Cells[0].Value.ToString() + "                 " + dataGridView1.CurrentRow.Cells[1].Value.ToString() + "                                                      " + Convert.ToInt32(dataGridView1.CurrentRow.Cells[2].Value.ToString()) + "                        " + Convert.ToInt32(dataGridView1.CurrentRow.Cells[3].Value.ToString()));
    }
    label7.Text = listBox1.Items.OfType<int>().ToArray().Sum().ToString();
}

您不必在单独的变量sum中跟踪从列表框中添加或删除的项目。只要你想对值进行求和,就在listBox上使用LINQ sum()操作。

listBox1.Items.OfType<int>().ToArray().Sum()

答案 2 :(得分:0)

您可以创建一个新类,例如,它可以是Item并向其添加一些行。

class Item 
{
    public static List<Item> Inventory = new List<Item>();

    public Int32 Id { get; set; }
    public Int32 Price { get; set; }
    public String ItemName { get; set; }

    private static Int32 NextId { get; set; } = 0;
    private static Int32 TotalPrice { get; set; }

    public Item(Int32 price, String name) 
    {
        this.Price = price;
        this.ItemName = name;   
        this.Id = NextId;   

        Item.Inventory.Add(this);
        Item.TotalPrice += price;

        Item.NextId++;
        // add to listbox or anything
        // like listBox1.Items.Add($"{this.Id} | {this.ItemName} / {this.Price}");
    }

    public static Int32 GetTotalPrice()
    {
        return Item.TotalPrice;
    }

    public static void RemoveItem(Int32 Id) 
    { 
        foreach ( Item _item in Item.Inventory )
        {
            if(_item.Id.Equals(Id)) 
            {
                Item.Inventory.Remove(_item);
                break;
            }
        }
    }
}

答案 3 :(得分:0)

要根据列表中的选定行扣除金额,请尝试以下操作:

ListBox.SlectedObjectCollection TheSelectedItem = new ListBox.SlectedObjectCollection(listBox1);

TheSelectedItem = listBox1.SelectedItems; 

int sum = 0;
private void button6_Click(object sender, EventArgs e) 
{
   //also ensure that the row is selected before clicking the button that's the only way the SelectedIndex click event will fire, pass the selected row value.
if(listBox1.TheSelectedItem.Count != 0)
{
   while (listBox1.SelectedIndex != -1)
     {   
        listBox1.Items.RemoveAt(listBox1.SelectedIndex);        
        sum -= Convert.ToInt32(dataGridView1.CurrentRow.Cells[3].Value.ToSt‌​ring()); 
      }           
  }
     label7.Text = sum.ToString();         
}