创建类,它将存储您要传递的值(我们称之为Foo
)。
Form2
那么应该有一个属性。在属性的 setter 中,设置控件:
public partial class Form2 : Form
{
private Foo _bar;
public Foo Bar
{
set
{
_bar = value;
//set your controls here
}
}
在编辑按钮上,设置如下属性:
Form2 form2 = new Form2();
form2.Bar = bar; //bar contains values to edit
然后在 上放一个保存按钮Form2
,这会将值从控件保存回该对象。
For every control I would have a field in Foo
class, eg. string
for textboxes, bool
for checkboxes and enum
or int
for comboboxes (where integer value would equal selected index).
Alternatively, you could use Dictionary
class instead and have key and value pair for every control.
You can also have some enum
, if your form looks or behaves differently in New and Edit mode.