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

后端 未结 3 2000
没有蜡笔的小新
没有蜡笔的小新 2021-01-22 05:00

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

3条回答
  •  借酒劲吻你
    2021-01-22 05:59

    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.

提交回复
热议问题