Trying to make changes on two different child form

2019-07-26 01:47发布

问题:

I have an application with three forms.

I want to be able to update the listview control when a new object is being created in a separate form when all three of them are displayed.

MainForm - Contains a List collection instantiated inside, two buttons where CarForm and ListViewCarForm are instantiated.

public static List<Cars> listOfCars = new List<Cars>();

public List<Cars> CarList
{
   get { return listOfCars; }
   set { listOfCars = value; }
}

private void displayListViewToolStripMenuItem_Click(object sender, EventArgs e)
{
   ListViewCarForm lvcf = new ListViewCarForm();
   lvcf.Show();
}

private void newCarFormButton_Click(object sender, EventArgs e)
{
   CarForm cf = new CarForm();
   cf.Show();

   //Adds new item created to listOfItems 
   cf.ObjectCreatedToList += ObjectCreatedToListCollectionHandler;
}

CarForm - Contains controls for the user to enter values, those values are stored inside a class member variable that is created as an object and is then added inside the List Collection in the MainForm

public EventHandler ObjectCreatedToList;

//Class Property that assigns values to member variables
public Cars Info
{
   get
   {
       //Instantiates new Cars class, assigns member variables to control values and returns new Cars object
   }
   set
   {
       //set control values to Cars member variables
   }  
}

private void addCarToolStripButton_Click(object sender, EventArgs e)
{
   if(ObjectCreatedToList != null)
   {
       ObjectCreatedToList(this, new EventArgs());
   }
   //Some Validation here to prevent control values to reset

   //If all control values are entered, this will clear the controls
   Info = new Cars();
}

ListViewForm - Contains a ListView where when items are being added into the List Collection in CarForm, it should also be added to the listview control

The problem I am having is when all three forms are opened, the listview control inside the ListViewForm should be updated as new objects are being created by the CarForm and added inside the List Collection inside the MainForm.

Since the two forms are being instantiated inside a different button in the MainForm, I can't seem to figure out how to add the items inside the listview control without it not hitting the method or giving me an error.

*This is my first time working with Windows Application Forms

回答1:

To keep this simple, one solution is to pass the variable you need to the child form. Example: assuming the main form contains a ListBox of items and a button to open a Controlsform, then, when the user clicks the “Open Control Form” button… the ControlsForm is open, it has a text box and a button. When the user types something into the text box and then clicks the “Add to Parent List” button, the text in the text box should be added to the parent form’s ListBox items. You can pass the ListBox or List<Car> or anything you need to the child form. Hope this makes sense.

The MainForm has a ListBox and a button. The ListBox on the form is named Form1ListBoxControl. When the user clicks the “Open Control Form” button, the variable Form1ListBoxControl is passed to the newly created ControlsForm then the ControlsForm is shown…As above

Main form “Open Control Form” button click event…

private void btnOpenControlsForm_Click(object sender, EventArgs e) {
  ControlsForm controlsForm = new ControlsForm(Form1ListBoxControl);
  controlsForm.Show();
}

ControlsForm...

To use the passed Form1ListBoxControl variable in the ControlsForm we need to make a global ListBox variable so the other methods in the form can use the parents ListBox

ListBox parentListBox;

In addition, we need to create a new "Constructor” for the ControlsForm to accept a ListBox parameter and use this parameter as the global variable defined above.…

public ControlsForm(ListBox passedParentListBox) {
  InitializeComponent();
  parentListBox = passedParentListBox;
}

Finally the button event in the ControlsForm to add new items to the parents ListBox

private void btnAddToParentList_Click(object sender, EventArgs e) {
  parentListBox.Items.Add(textBox1.Text);
}