我想从组合框中选择项目的数量并自动在列表框中计算价格 [英] i want select item's quantity from combo-box and calculate price in list box automatically

查看:126
本文介绍了我想从组合框中选择项目的数量并自动在列表框中计算价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要从组合框中选择项目的数量并自动在列表框中计算价格.
我如何在C#中做到这一点?

我为餐厅创建了一个用于billing.so的项目,因此我只通过输入数量表格组合框就可以计算出价格.

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.

推荐答案

即使所需行为的描述确实含糊不清而且不清楚,您只需要知道一个主意.

关键是:不要将字符串存储为组合框列表部分的元素.任何其他类型的对象都可以是元素.唯一的问题是:当在UI中显示时,项目中将显示什么文本?答案是:方法ToString返回的结果.因此,解决方案是:在结构或类中重写虚拟方法Object.ToString以用作列表元素.

首先,您需要为项目创建一些类以存储在列表中.您的需求意味着它具有双重目的:在列表中显示一些文本,并提供图像显示信息.假设这是图像文件名(可能更复杂;这仅是示例).要在列表中显示该项目,您需要使用它来覆盖object.ToString():

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():

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:

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



每次需要一些与选定项目相关的数据时,都需要键入将选定项目强制转换为ListItemHelper并在计算中使用数据.

您应该自己做其他事情.您甚至没有指定要使用的UI库(请参阅我对问题的评论),因此进一步的代码示例将毫无意义.我什至不知道您正在使用的ComboBox的确切类型.提出问题时,请始终指定所有相关详细信息.

—SA



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


这篇关于我想从组合框中选择项目的数量并自动在列表框中计算价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆