(c# + windows forms) Adding items to listBox in different class

☆樱花仙子☆ 提交于 2019-12-02 01:32:06

Declare RentalId property on Form2. And at CarRental form (your first form) do following:

using(Form2 form2 = new Form2())
{
    if (fomr2.ShowDialog() != DialogResult.OK)
        return;

    listBox.Items.Add(form2.RentalId);
}

Implement Fomr2.RentalId property this way:

public string RentalId
{
   get { return idRental.Text; } // you don't need ToString() call
}

Then select your "Accept" button and set its DialogResult property to OK. Thus clicking on that button will close your dialog form and return DialogResult.OK.

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.

You need to access the open form instead of creating new instance of CarRental form

private void button1_Click(object sender, EventArgs e)
{
    CarRental i = (CarRental)Application.OpenForms["CarRentalFormObjectName"];
    string id = idRental.Text.ToString();

    i.listBox1.Items.Add(id);
    i.listBox1.Update();
    this.Close();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!