Home > OS >  How can I create an object in a form and add it to another form's list?
How can I create an object in a form and add it to another form's list?

Time:12-02

I have form1 which looks like this:

form1

and AddSnackForm which looks like this:

AddSnackForm

How can I add snacks using AddSnackForm and put it in the list in Form1 ? I have tried using "using" statement but it just added a snack with default values Snack("", 0, 0).

Code for Form1:

public partial class Form1 : Form
{
    SnackBar snackBar;
    public Form1()
    {
        snackBar = new SnackBar(new Snack("Chocolate", 2, 10), 
                                         new Snack("Soda", 1.5, 10), 
                                         new Snack("Chips", 3, 20));
        InitializeComponent();
        lbSnacks.Items.Clear();
        foreach(Snack snack in snackBar.Snacks)
        {
            lbSnacks.Items.Add(snack);
        }
        
    }

    private void BtnOrder_Click(object sender, EventArgs e)
    {
        double orderPrice = snackBar.ProcessOrder(int.Parse(cbSnackOne.Text), int.Parse(cbSnackTwo.Text), int.Parse(cbSnackThree.Text));
        if (orderPrice > 0)
        {
            snackBar.Revenue  = orderPrice;
            MessageBox.Show($"Your order costs {orderPrice:f2}€.");
        }
        else
        {
            switch (orderPrice)
            {
                case -1:
                    MessageBox.Show("Snack One not in stock. Your order cannot be processed.");
                    break;
                case -2:
                    MessageBox.Show("Snack Two not in stock. Your order cannot be processed.");
                    break;
                case -3:
                    MessageBox.Show("Snack Three not in stock. Your order cannot be processed.");
                    break;
            }
        }
    }

    private void BtnAdd_Click(object sender, EventArgs e)
    {
        Snack snack = new Snack("", 0, 0);
        using (AddSnackForm add = new AddSnackForm()) ;
        
        add.Show();
        snack = add.snack;
            
        snackBar.Snacks.Add(snack);
        lbSnacks.Items.Add(snack);
    }

Code for AddSnackForm:

 public partial class AddSnackForm : Form
{
    public Snack snack = new Snack("", 0, 0);
    public AddSnackForm()
    {
        InitializeComponent();
    }

    private void BtnAdd_Click(object sender, EventArgs e)
    {
        snack.Name = tbName.Text;
        snack.Price = double.Parse(tbPrice.Text);
        snack.InStockAmount = int.Parse(tbAmount.Text);
        Close();
    }
}

CodePudding user response:

In my experience there are two different approaches:

  1. Pass the listbox from form1 to AddSnackForm as reference:

Form1:

AddSnackForm add = new AddSnackForm(Listbox lbSnacks)

AddSnackForm:

  public AddSnackForm(Listbox lbSnacks)
        {
            InitializeComponent();
            _lbSnacks = lbSnacks;
        }
// methods to add elements to lbSnacks list

And this is the simplest approach i guess...

  1. Using a callback, it's a bit more complicated but a cleaner solution in my opinion.
  • Related