简体   繁体   中英

How to add the selected ListBox Item along with it's price on a receipt to be printed?

I connected the ListBox with a table from a local database, with two columns:
name and price.

I want the Item selected from the ListBox to be send. The Item 's name and price should be added to the price of the next selected Item and the receipt should be printed.

How can I do that?

You will have to add more logic to fit your specific needs but the below code should serve as general method to achieve what you are wanting to accomplish.

    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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM