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
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 aControlsform
, then, when the user clicks the “Open Control Form” button… theControlsForm
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’sListBox
items. You can pass theListBox
orList<Car>
or anything you need to the child form. Hope this makes sense.The
MainForm
has aListBox
and a button. TheListBox
on the form is namedForm1ListBoxControl
. When the user clicks the “Open Control Form” button, the variableForm1ListBoxControl
is passed to the newly createdControlsForm
then theControlsForm
is shown…As aboveMain form “Open Control Form” button click event…
ControlsForm...
To use the passed
Form1ListBoxControl
variable in theControlsForm
we need to make a globalListBox
variable so the other methods in the form can use the parentsListBox
…In addition, we need to create a new "Constructor” for the
ControlsForm
to accept aListBox
parameter and use this parameter as the global variable defined above.…Finally the button event in the
ControlsForm
to add new items to the parentsListBox
…