Click here to Skip to main content
15,888,330 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
Hello Guys am working on a project an i am facing a problem if i am adding a item in listbox so items add successfully and now i want if i add same items two times so its not add 2 times it's add 1 times but it's show quantity with mulitplication amount.

i am using this code for adding items in listbox
C#
Button b = (Button)sender;
                 tblProduct tp2 = (tblProduct)b.Tag;
                 string product2 = tp2.productName;
                 listBox2.Text = tp2.ToString();
                 products2.Add(tp2);
                 total2 += (decimal)tp2.productPrice;



Please look at this image:
Imgur: The most awesome images on the Internet[^]

What I have tried:

int Qlty = +1;
string ProductName = ((tblProduct)e.ListItem).productName;
string Price = "Rs: " + String.Format("{0:}", ((tblProduct)e.ListItem).productPrice);
string ProductNamePadded = ProductName.PadRight(33);
e.Value = ProductNamePadded+Qlty+Price;
Posted
Updated 8-Aug-16 23:48pm
v4
Comments
Richard MacCutchan 7-Aug-16 8:09am    
You need to check if the item is already in the ListBox.
Member 9983063 7-Aug-16 10:09am    
exactly i need this.But i dont know how to do this please help me about this
Member 9983063 7-Aug-16 12:38pm    
sir i found the solution of how to check if the item is already in the listbox
here is the solution
var duplicates = products.GroupBy(i => i.productName)
.Where(x => x.Count() > 1)
.Select(val => val.Key);

foreach (var item in duplicates)
{
//now please tell me which code i write here that solve issue
}

1 solution

I would design the objects as follows. This solution can be copied to a console app to test it.
See it working here[^]

Notice I've added a stub method for you to check if an item is already added, although the following logic should help you avoid having to check manually and tediously if something is already in a list.

C#
using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
	public void Main()
	{
		// Simulate order
		Order order = new Order
		{
			TableNumber = "Table 1",
			Products = new List<Product>
			{
				new Product { Name = "Item 1", Price = 100.00m, Quantity = 3},
				new Product { Name = "Item 2", Price = 89.00m, Quantity = 2},
				new Product { Name = "Item 3", Price = 50.00m, Quantity = 1 }
			}
		};
		
		
		foreach(var p in order.Products)
		{
			Console.WriteLine(p.Name + " (Each: " + p.Price + ")" + " - Qty: " + p.Quantity + " (" + p.Total() + ")");
		}
	}
}

public class Order
{
	public List<Product> Products { get; set; }

	public string TableNumber{get;set;}
	
	public decimal Total()
	{
		if (Products.Any())
			return Products.Sum(x => x.Price);
		return 0.00m;
	}

	public Order()
	{
		Products = new List<Product>();
	}
	
	public void RemoveProduct()
	{
		// Implement here remove logic
	}
	
	public bool IsAdded(string productName)
	{
		return Products.Any(x=>x.Name.ToLower() == productName.ToLower());
	}
}

public class Product
{
	public string Name { get; set; }
	public decimal Price { get; set; }
	public int Quantity { get; set; }
	
	public decimal Total()
	{
		return Price * Quantity;
	}
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900