简体   繁体   中英

Update textbox control count of items inside a list<> when it's cleared using Custom Events C#

I have the MainForm that contain two read-only textboxes and two buttons that either opens up as many instances of PizzaCreatorForms the user wants shown and the other buttons displays one instance of a PizzaOrdersListViewForm. The two read-only textboxes keep track of how many instance of the PizzaCreatorForm are shown (this part works) and the other keeps track of how many items are being created (this works, too).

I should add in that every object created by PizzaCreatorForm is added in the List<>, as well, as in the PizzaOrdersListViewFrom listView.Items control. There is also a clear button inside the MainForm and in the PizzaOrderListViewForm that clears the List<>.

My code works, but I ran into two problems.

  1. Whenever a new object is added inside the list<>, it starts at 0 when I want it to start at 1.

  2. Whenever I clear the List<> using the clear button inside PizzaOrderListViewForm, the listview control items are cleared, but the textbox displays the same number

This is the custom event I tried to use for #2:

MainForm

    public partial class Form1 : Form
    { 
       public static List<Pizza> pizzaList = new List<Pizza>();
       public List<Pizza> PizzaList
       {
           get { return pizzaList; }
           set { pizzaList = value; }
       }

       //Where PizzaOrdersListViewForm is instantiated
       private void displayOrdersToolStripMenuItem_Click(object sender, EventArgs e)
       {
           PizzaOrdersListViewForm orderListV = new PizzaOrdersListViewForm();


       }
       //Where PizzaCreatorForm is instantiated
       private void newPizzaOrder_Click(object sender, EventArgs e)
       {
           PizzaCreatorForm pizzaForms = new PizzaCreatorForm(this);
           pizzaForms.PizzaInList += PizzaInListHandler;
           pizzasCreatedCounterTextBox.TextChanged += PizzaInListHandler;
       }

       public void PizzaInListHandler(object sender, EventArgs e)
       {
           pizzasCreatedCounterTextBox.Text = pizzaList.Count.ToString();        
       }
       public void ClearMainListChangeHandler(object sender, EventArgs e)
       {  
           pizzaList.Clear();
       }
    }

PizzaCreatorForm

    public partial class PizzaCreatorForm : Form
    {
       public EventHandler PizzaInList;

       public Pizza Order
       {
           //Code that assigns Pizza class member variables to control values
       }

       Form1 MainForm = null;

       public PizzaCreatorForm(Form1 mForm)
       {
            InitializeComponent();
            MainForm = mForm
       }

       private void orderNewPizaaToolStripButton_Click(object sender, EventArgs e)
       {
            if(PizzaInList != null)
            {
                PizzaInList(this, new EventArgs());
            }

            MainForm.PizzaList.Add(Order);

            Order = new Pizza();
       }
    }

PizzaOrdersListViewForm

    public partial class PizzaOrdersListViewForm : Form
    {
       public EventHandler ClearMainList;

       Form1 mainForm = null;

       public PizzaOrdersListViewForm(Form1 TheMain)
       {
          InitializeComponent();
          mainForm = TheMain;
       }

       private void clearToolStripButton_Click(object sender, EventArgs e)
       {
          if(ClearMainList != null)
          {
              ClearMainList(this, new EventArgs());
          }
          ClearMainList += mainForm.ClearMainListChangeHandler;
          pizzaListView.Items.Clear();

       }

    }

All I had to do was subscribe the PizzaOrdersListViewForm ClearMainList EventHandler to the custom PizzaInListHandler method inside the displayOrdersToolStripMenuItem_Click method in the MainForm.

private void displayOrdersToolStripMenuItem_Click(object sender, EventArgs e)
{
     PizzaOrdersListViewForm orderListV = new PizzaOrdersListViewForm();
     orderListV.ClearMainList += ClearMainListChangeHandler;
     orderListV.ClearMainList += PizzaInListHandler;
}

I figured out my mistake with the pizzaList.Count. I accidentally used the Count that counted the sequence of each item when I should have choosen the Count that brings back the total of elements inside the List<>.

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