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

标签 c# winforms

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

我想发送从 ListBox 中选择的 ItemItem 的名称和价格应添加到下一个所选 Item 的价格中,并且应打印收据。

我该怎么做?

最佳答案

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

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

关于c# - 如何在要打印的收据上添加选定的 ListBox 项目及其价格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56908495/

相关文章:

c# - 是否有可能在任何任务因异常和日志而终止时捕获?

c# - 基于具有逗号分隔值的列创建多个 DataTable 行

c# - 为什么 [SetUpFixture] 中的 [OneTimeSetUp] 在 (Test,TestCaseSource()) 之后调用

c# - 多线程和 lambda 变量范围

c# - 仅在 Gridview 的最后一行显示复选框?

C# Windows 窗体被任务管理器杀死......有没有办法运行关机功能?

c# - 如何使用C#在mysql数据库中存储图像路径

c# - 使用 winform checkbox 进行 linq 查询

c# - 如何在运行时查找窗体中的所有控件

c# - 将一种形式的控件用于另一种形式