Click here to Skip to main content
16,017,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want select item's quantity from combo-box and calculate price in list box automatically.
how i doing that in c#?

i made project for restaurant for billing.so i have calculate price from just enter the quantity form combo-box.
Posted
Updated 31-Dec-11 8:17am
v2
Comments
Umairbaig 31-Dec-11 12:58pm    
your statements are ambiguous, please clear them. thank you
Umairbaig 31-Dec-11 13:00pm    
Where do you get Price of selected item?
Sergey Alexandrovich Kryukov 31-Dec-11 16:43pm    
Tag the type of application and UI library: WPF, Forms, ASP.NET, what?
--SA

1 solution

Even though the description of desired behavior is really ambiguous and not clear, you need to know just one idea.

The thing is: don't store string as the element of the list part of combo box. An object of any other type can be an element. The only problem is: what text will be shown in the item when it is presented in UI? The answer is: whatever is returned by the method ToString. So, the solution is: override the virtual method Object.ToString in your structure or class to be used as the list element.

First, you need to create some class for an item to store in the list. You requirements mean that it serve dual purpose: to show some text in the list and also to provide information for image presentation. Let's assume this is image file name (could be more complex; this is just for example). To show this item in the list, all you need it to override object.ToString():

C#
internal class ListItemHelper {
    internal ListItemHelper(string itemText, double data) {
        this.ItemText = itemText;
        this.fData = data;
    } //ListItemHelper
    public override string ToString() { //will be shown in UI
        return ItemText;
    } //object.ToString()
    internal double Data { get {return fData; } }
    string ItemText;
    double fData;
} //class ListItemHelper


Use this class to add information to your list:

C#
MyList.Add(new ListItemHelper("Payment", 21.52));


Every time you need some data associated, say, with selected item, you need to type cast the selected item to ListItemHelper and use data in your calculations.

You should do everything else by yourself. You did not even specify the UI library you use (please see my comment to the question), so further code samples would make no sense. I don't even know exact type of ComboBox you are using. Always specify all relevant detail when asking a question.

—SA
 
Share this answer
 

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