说,如果我有自己的数据类定义如下:
public class MyData
{
public string v1;
public int v2;
//etc.
}
我可以将它传递给我自己的 Form 派生类并使用它来初始化表单控件:
public partial class MyForm : Form
{
private MyData dataIn;
public MyForm(MyData data)
{
InitializeComponent();
dataIn = data;
}
}
private void MyForm_Load(object sender, EventArgs e)
{
//Init controls in 'MyForm' from 'dataIn'
}
private void buttonSave_Click(object sender, EventArgs e)
{
//Save button was clicked
//Read data from 'MyForm', but
//how to return it back in 'MyData' class?
//Close form
this.DialogResult = System.Windows.Forms.DialogResult.OK;
this.Close();
}
表单本身就是这样调用的:
//Initial data for the form
MyData data = new MyData(){
v1 = "test",
v2 = 123
};
MyForm dlg = new MyForm(data);
if (dlg.ShowDialog() == DialogResult.OK)
{
//User clicked Save -- how to get data back in 'MyData'?
}
问题是如何让我MyData
的数据填充并从中返回MyForm
?