我想做这样的事情:
主要表格:
Car car = new Car(); //create new object
Form form2 = new Form(car); //Create new form and pass the car that I've created a line before.
form2.Show(); //Show the second Form
Form2:
public Car(Car car)
{
InitializeComponent(); //here the car is visible, in private method below, for the button, I can't get to it.
}
private void button(object sender, EventArgs e)
{
car.maker = "VW" //update the information about the car. I can't make this line to work, variable "car" is unavailable here, how to get to it?
this.Close(); //close second form
}
当我在 form2 中添加有关汽车的所有信息时,如何在 form2 中获取此对象并将其返回到 MainForm?
在form2中创建一个属性
public Car MyCar {get; set;}
并设置这个属性
Car car = new Car(); //create new object
Form form2 = new Form();
form2.MyCar=car;
form2.ShowDialog();
// get car here again
Car car2 =form2.MyCar;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)