简体   繁体   中英

How Do I Append Items to a Listbox in Another Window Whenever I Click a Button in C#?

I'm working on a WPF app on a Wawa touchscreen simulator. Whenever I click on a button in the menu (eg, a beverage), the item's name and price will be displayed on a Listbox in another window (eg, "Frozen Smoothie", 1.99). The images below show what's happening.

在此处输入图像描述

However, if I select another item (eg, a frozen cappuccino), that item's information replaces the previous item I selected.

“冰冻卡布奇诺”取代“冰冻冰沙”

I've made a Receipt class that takes the food item's name and price to create a Receipt object called foodItem, which is added to the Listbox.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Wawa_TouchScreen
{
public class Receipt
    {
    private string foodItemName;
    private double price;

    public Receipt(string foodItemName, double price) 
        {
        this.foodItemName = foodItemName;
        this.price = price;
        }

    public override string ToString()
        {
        string foodItem = foodItemName + " $" + price;
        return foodItem.ToString();
        }

    public string getFoodItemName() 
        {   
        return foodItemName;
        }

    public double getPrice() 
        {
        return price;
        }
    }
}

Here's the code for the beverage window:

public partial class Window1 : Window
    {
    public string foodItemName;
    public double price;

    private void frozenSmoothieBtn_Click(object sender, RoutedEventArgs e)
        {
        foodItemName = "Frozen Smoothie";
        price = 1.99;

        Receipt foodItem = new Receipt(foodItemName, price);

        MessageBox.Show("You have chosen a frozen smoothie.", foodItemName);
        Window2 checkOut = new Window2();
        checkOut.Show();
        this.Visibility = Visibility.Hidden;
        checkOut.lstBoxReceipt.Items.Add(foodItem);
    }

    private void frozenCappuccinoBtn_Click(object sender, RoutedEventArgs e)
        {
        foodItemName = "Frozen Cappucino";
        price = 1.99;

        Receipt foodItem = new Receipt(foodItemName, price);

        MessageBox.Show("You have chosen a frozen cappuccino.", foodItemName);
        Window2 checkOut = new Window2();
        checkOut.Show();
        this.Visibility = Visibility.Hidden;
        checkOut.lstBoxReceipt.Items.Add(foodItem);
        }
    }
}

I'd appreciate any help you can give. Thanks!

To provide an immediate fix to your issue, you need to create a shared list (orderList) to bind to the lstBoxReceipt Listbox. All operations of adding and removing the Receipt types should be performed on the shared list.

We are using an ObservableCollection instead of List because if you use a List your changes to the underlying list object will not be immediately displayed on the Listbox. It will only display the first item.

Also, try improving your code by refactoring the button click events to call a method that adds the receipt to the list and another method that displays the checkout window. Instantiate the checkout window once by moving the window creation code outside the event code.

public partial class MainWindow : Window
{

    public string foodItemName;
    public double price;
    
    //Add a shared list that you operate on adding and removing your food items from                   
    //then assign it to the lstBoxReceipt.ItemsSource
    ObservableCollection<Receipt> orderList = new ObservableCollection<Receipt>();

    //Instantiate the checkOut window
    Window2 checkOut = new Window2();

    private void frozenSmoothieBtn_Click(object sender, RoutedEventArgs e)
    {
        foodItemName = "Frozen Smoothie";
        price = 1.99;

        Receipt foodItem = new Receipt(foodItemName, price);

        MessageBox.Show("You have chosen a frozen smoothie.", foodItemName);

        orderList.Add(foodItem);

        checkOut.Show();
        this.Visibility = Visibility.Hidden;
        
        //Add the order list as the data source of the listbox.
        checkOut.lstBoxReceipt.ItemsSource = orderList;
        
    }

    private void frozenCappuccinoBtn_Click(object sender, RoutedEventArgs e)
    {
        foodItemName = "Frozen Cappucino";
        price = 1.99;

        Receipt foodItem = new Receipt(foodItemName, price);

        MessageBox.Show("You have chosen a frozen cappuccino.", foodItemName);

        orderList.Add(foodItem);

        checkOut.Show();
        this.Visibility = Visibility.Hidden;
        
        //Add the order list as the data source of the listbox.
        checkOut.lstBoxReceipt.ItemsSource = orderList;
    }
}

I'm working on a WPF app on a Wawa touchscreen simulator. Whenever I click on a button in the menu (eg, a beverage), the item's name and price will be displayed on a Listbox in another window (eg, "Frozen Smoothie", 1.99). The images below show what's happening.

在此处输入图像描述

However, if I select another item (eg, a frozen cappuccino), that item's information replaces the previous item I selected.

“冷冻卡布奇诺”取代“冷冻冰沙”

I've made a Receipt class that takes the food item's name and price to create a Receipt object called foodItem, which is added to the Listbox.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Wawa_TouchScreen
{
public class Receipt
    {
    private string foodItemName;
    private double price;

    public Receipt(string foodItemName, double price) 
        {
        this.foodItemName = foodItemName;
        this.price = price;
        }

    public override string ToString()
        {
        string foodItem = foodItemName + " $" + price;
        return foodItem.ToString();
        }

    public string getFoodItemName() 
        {   
        return foodItemName;
        }

    public double getPrice() 
        {
        return price;
        }
    }
}

Here's the code for the beverage window:

public partial class Window1 : Window
    {
    public string foodItemName;
    public double price;

    private void frozenSmoothieBtn_Click(object sender, RoutedEventArgs e)
        {
        foodItemName = "Frozen Smoothie";
        price = 1.99;

        Receipt foodItem = new Receipt(foodItemName, price);

        MessageBox.Show("You have chosen a frozen smoothie.", foodItemName);
        Window2 checkOut = new Window2();
        checkOut.Show();
        this.Visibility = Visibility.Hidden;
        checkOut.lstBoxReceipt.Items.Add(foodItem);
    }

    private void frozenCappuccinoBtn_Click(object sender, RoutedEventArgs e)
        {
        foodItemName = "Frozen Cappucino";
        price = 1.99;

        Receipt foodItem = new Receipt(foodItemName, price);

        MessageBox.Show("You have chosen a frozen cappuccino.", foodItemName);
        Window2 checkOut = new Window2();
        checkOut.Show();
        this.Visibility = Visibility.Hidden;
        checkOut.lstBoxReceipt.Items.Add(foodItem);
        }
    }
}

I'd appreciate any help you can give. Thanks!

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