Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Okay, so first of all, you're going to need to store something else in <code>lstStock.</code> I'd suggest something like this:</p> <pre><code>public class StockItem { public string Name { get; set; } public int Count { get; set; } } </code></pre> <p>Next, I'd suggest that you don't use the <code>.Items</code> field as the actual container where you're storing your stock items. Instead, I'd have a <code>List&lt;StockItem&gt;</code> that you use to populate <code>lstSupply</code> when you refresh.</p> <pre><code>private List&lt;StockItem&gt; _items = new List&lt;StockItem&gt;(); </code></pre> <p>Then, when you click "add", you do two things:</p> <ol> <li><p>Iterate through <code>_items</code> to see if there is already a stock item in there of the matching supply. Create one, and add it, if it does not exist.</p></li> <li><p>Find the matching stock item in <code>_items</code> and increment its <code>Count</code>.</p></li> </ol> <p>To get it to display the way you'd like, we can override <code>ToString()</code> in our <code>StockItem</code> class.:</p> <pre><code>public class StockItem { public string Name { get; set; } public int Count { get; set; } public override string ToString() { if (Count &gt; 1) return Name + " x" + Count; return Name; } } </code></pre> <p>Finally, whenever any changes are made, you simply re-populate <code>lstStock</code>:</p> <pre><code>private void RefreshGui() { lstStock.Items.Clear(); lstStock.Items.AddRange(_items.ToArray()); } </code></pre> <p>There are plenty of other ways of going about this, but for this simple exercise, this is how I think I would do it.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload