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