I have two classes(forms), and I would like an item from class2
to be added to listBox
in class1
when I click "Accept" button.
I tried with the following code, but nothing changes in the listBox:
private void button1_Click(object sender, EventArgs e)
{
CarRental i = new CarRental();
string id = idRental.Text.ToString();
i.listBox1.Items.Add(id);
i.listBox1.Update();
this.Close();
}
Where did I make the mistake?
you created a new entity of type CarRental. what you should do is to send the first form to the second on construct, and modify things through that instance.
Declare
RentalId
property onForm2
. And atCarRental
form (your first form) do following:Implement
Fomr2.RentalId
property this way:Then select your "Accept" button and set its
DialogResult
property toOK
. Thus clicking on that button will close your dialog form and returnDialogResult.OK
.You need to access the open form instead of creating new instance of CarRental form