将对象传递给新表单 [英] Pass object to new form
本文介绍了将对象传递给新表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想做这样的事情:
主窗体:
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
表格 2:
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中找到这个对象并将其返回给MainForm,当我在form2中添加有关汽车的所有信息时?
How to get to this object in form2 and give it back to MainForm, when I'll add all the info about the car in form2?
推荐答案
在 form2 中创建属性
Create a property in 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;
这篇关于将对象传递给新表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文