views:

279

answers:

2

Hi,

I am using c# windows forms. I have multiple buttons linked to a listview which when a button is pressed, a new item is added to the listview. The column headers in the listview are 'Name' and 'Amount'. When a different button is pressed, a different item is added to the listview. The thing i need help with is as follows: When the same button is pressed twice, I want the amount to go from "1" to "2" on the second click. So the item name isnt duplicated but the amount is increase. The problem is I am using text to link the button to the linklist at the moment e.g. ("Coca Cola", "1") which adds the item name as coca cola and the amount as 1. I know it is something to do with integers so please help!!

Thanks

+1  A: 

When the user presses a button, before adding the new row, just loop through all the current ListViewItems and check if any of them already have the same name, if so increment the amount column. Otherwise add the row as you currently do.

bool found = false;
foreach (ListViewItem item in listView1.Items)
{
     if (item.Text.Equals("Coke"))
     {
          int amt = int.Parse(item.SubItems[1].Text);
          amt++;
          item.SubItems[1].Text = amt.ToString();
          found = true;
     }
}
if (!found)
{
     ListViewItem item = listView1.Items.Add("Coke");
     item.SubItems.Add("1");
}
ho1
thank you this has worked perfectly. I have a printitems method which is linked to a list created in a seperate class. how do i add the item to the amount column? the code is below: private void printItems(Items items) { ListViewItem item = new ListViewItem(); item.Text = items.ItemName; item.SubItems.Add(items.AmountOfItem); listView1.Items.Add(item); }
Richard
Don't really understand your question so do you mean that you want to store the actual list of PrintItems in each item in the ListView?If so, just do something like `item.Tag = items;` where item is the ListViewItem that was just created. Or if you want to store it in the amount column, when you created the amount subitem just add it to it's tag like 'item.SubItems.Add(items.AmountOfItem).Tag = items;` .
ho1
@Richard; the code given by ho *is* your new printItems method - except you'll need to replace the hard-coded "Coke" with your own (items) value.
riffnl
+1  A: 
var amount = new Dictionary<string, int>();

Button1_Click()
{
    if(amount["Coca Cola"]<=0)
    {
        add a listview items with amount 0
    }
    // find the listitem with the value "Coca Cola" using FindItemWithText() Method
    // set the value of ++amount["Coca Cola"] to that listitem in the amount field
}

Having a map in hand will avoid reading reading through the listview items in other scenarios.

Veer
Out of genuine interest, why did you use type inference in that example?
Martin Milan
@Martin Milan: Why shouldn't I?
Veer
No real reason you can't - but I'm wondering why you DID...
Martin Milan
@Martin Milan: just to reduce some code. You may again complaint me that c# compiler will not infer types outside methods. That's just to tell the QA that amount is of type Dictionary.
Veer
I'm not complaining Veer - I'm discussing... Just interested in your approach. I didn't realise about type inference not inferring outside of methods - but then I've not done much with type inference myself.
Martin Milan