如何在要打印的收据上添加所选的ListBox项目及其价格?

时间:2019-07-05 19:42:19

标签: c# winforms

我将ListBox与本地数据库中的表连接起来,该表有两列:
名称和价格。

我希望发送从Item中选择的ListBoxItem的名称和价格应添加到下一个选定的Item的价格中,并应打印收据。

我该怎么做?

1 个答案:

答案 0 :(得分:1)

您将不得不添加更多逻辑以满足您的特定需求,但是以下代码应作为实现您要完成的目标的一般方法。

    public class DBRowObject { // The object that will be stored in the "DataSource" of the ComboBox
        public int iPrice = 0;
        public string strName = "";

        public DBRowObject(int price, string name) {
            iPrice = price;
            strName = name;
        }

        public override string ToString() // This means the combo box will display the name
        {
            return strName;
        }

    }

    public Form1()
    {
        InitializeComponent();
        List<DBRowObject> lsRows = new List<DBRowObject>(){new DBRowObject(3,"Bob"),new DBRowObject(2,"Sam"),new DBRowObject(5,"John")};
        this.cbCombo.DataSource = lsRows;
    }
    public DBRowObject prevSelected = null;
    private void cbCombo_SelectedValueChanged(object sender, EventArgs e)
    {
        DBRowObject dbrCurr = (DBRowObject)cbCombo.SelectedItem;

        if (prevSelected != null) {
            dbrCurr.iPrice += prevSelected.iPrice;
        }

        // TODO Display information about these objects and perform various other tasks

        prevSelected = dbrCurr;
    }