Pass object to new form

前端 未结 1 617
生来不讨喜
生来不讨喜 2021-01-29 06:23

I would like to do something like that:

MainForm:

Car car = new Car(); //create new object
Form form2 = new Form(car); //Create new form and pass the car         


        
相关标签:
1条回答
  • 2021-01-29 06:56

    Create a property in form2

    public Car MyCar {get; set;}
    

    and set this property

    Car car = new Car(); //create new object
    Form form2 = new Form(); 
    form2.MyCar=car;
    form2.ShowDialog(); 
    // get car here again
     Car car2 =form2.MyCar;
    
    0 讨论(0)
提交回复
热议问题