我在Form1中定义了一个类
public class Conditions
{
public string name { get; set; }
public int probability { get; set; }
public DateTime start_time { get; set; }
public DateTime end_time { get; set; }
public int age_min { get; set; }
public int age_max { get; set; }
public bool meldpeld { get; set; }
public bool onea { get; set; }
public bool oneb { get; set; }
public int gender { get; set; } // 0 - both, 1 - male, 2 - female
public int meld_min { get; set; }
public int meld_max { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我正在制作一个新的清单
List<Conditions> newconditions = new List<Conditions>();
Run Code Online (Sandbox Code Playgroud)
然后,我正在调用Form2
Conditions newconds = new Conditions();
Form2 form2 = new Form2(newconds);
form2.Show();
form2.TopMost = true;
Run Code Online (Sandbox Code Playgroud)
在Form2中,我有
public Form2(Form1.Conditions newcond)
{
InitializeComponent();
comboBox1.SelectedIndex = 2;
}
Run Code Online (Sandbox Code Playgroud)
我可以在那里使用set something for newcond
但是,我想要做的是在Form2中调用另一个函数
private void button2_Click(object sender, EventArgs e)
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚如何在该函数中使用newcond.我一定错过了一些明显的东西,对吗?
这也是一个好方法吗?基本上我想要做的是让用户定义任意数量的条件(他们可以添加,编辑,删除),然后在运行程序时使用这些条件.
谢谢
你是在正确的轨道上.
基本上,我会将您的Conditions类移动到它自己的名为Conditions.cs的文件中 - 这是最佳实践.
然后在类文件中为Form2定义一个成员变量.然后在Form2的构造函数中设置该成员变量.
private Conditions _conditions;
public Form2(Conditions cond)
{
_conditions = cond;
InitializeComponent();
comboBox1.SelectedIndex = 2;
}
Run Code Online (Sandbox Code Playgroud)
然后你可以在你的点击方法中使用它:
protected void button2_click(object sender, EventArgs args)
{
//Do things with _conditions
}
Run Code Online (Sandbox Code Playgroud)