简体   繁体   English

从多个文本框将多个项目添加到列表框

[英]Add multiple items into a ListBox from multiple TextBoxes

I want to create a basket for a product and the user enters the product name, quantity and price and then to be put into a list box on the same line. 我想为产品创建一个购物篮,用户输入产品名称,数量和价格,然后放入同一行的列表框中。 Here is what I have so far 这是我到目前为止的

private void addToBasket_Click(object sender, EventArgs e)
    {

        string product = productName.Text;
        decimal quan = quantity.Value;
        int quant = Convert.ToInt32(quan);
        string p = this.price.Text;
        int price = Convert.ToInt32(p);
        basket.Items.Add(new ListBoxItem(product, quan, price));
    }
}

internal class ListBoxItem
{
    private string product;
    private decimal quan;
    private int price;

    public ListBoxItem(string product, decimal quan, int price)
    {
        this.product = product;
        this.quan = quan;
        this.price = price;
    }
}

When I click add it puts in the list box: BasketForm.ListBoxItem with no values relating to what I entered into the text box 当我单击添加时,它将放入列表框:BasketForm.ListBoxItem,其中没有与我在文本框中输入的内容相关的值

When objects are bound to a (listbox) control, said control will by default use the ToString method to display the value. 当对象绑定到(列表框)控件时,默认情况下,该控件将使用ToString方法显示该值。 You could just override said method in your ListBoxItem class to make any control display the quantity, the item name and the unit price: 您可以在ListBoxItem类中重写上述方法,以使任何控件显示数量,项目名称和单价:

    public override string ToString()
    {
        return string.Format("{0}x {1} @{2}", quan, product, price);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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