我正在尝试将一个表单的组合框选择项调用到另一个表单构造函数。这样我就可以为其分配一些变量并将其用于其他目的。
包含 comboboxvalue 的表单是 Mode Form:
public Mode()
{
InitializeComponent();
}
private void Mode_Load(object sender, EventArgs e)
{
DataSet ds = new DataSet();
comboBox1.Focus();
String query = "select [test_no],[test_name] from [Test]";
ds = db.ExecuteDataSet(query);
comboBox1.DisplayMember = "test_name";
comboBox1.ValueMember = "test_no";
comboBox1.DataSource = ds.Tables["tablename"];
panel3.Controls.Add(comboBox1);
panel3.Controls.Add(Runbutton);
}
private void Runbutton_Click_2(object sender, EventArgs e)
{
label3.Enabled = true;
val = Convert.ToString(comboBox1.Text);
Test test = new Test(val);
Test test1 = new Test();
test1.Activate();
test1.Focus();
this.Hide();
test1.ShowDialog();
}
private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
{
val = Convert.ToString(comboBox1.SelectedItem);
}
测试形式:代码
string parentform;
public Test()
{
InitializeComponent();
RunFirst_Settings();
UserLogin login = new UserLogin();
login.Hide();
login.Visible = false;
}
public Test(string Mode)
{
parentform = Mode;
}
private void Startbutton_Click(object sender, EventArgs e)
{
stopwatch.Start();
timer1.Start();
ts=new ThreadStart(ProcessStarted);
th=new Thread(ts);
th.Start();
}
private void ProcessStarted()
{
if (parentform != null)
{
// Here every time it returns a null value. Before it was
// showing the exact selectedItem of combobox in Mode form:
MMTest(parentform);
}
else
{
//other code to be done
}
}
现在我怎样才能得到组合框的确切 selectedItem ?有什么建议吗?如果我只在模式表单中的 Runbutton_Click_2() 中调用 Test(string Mode) 构造函数,它会显示空白表单,而不是实际的测试表单。
我的错误在哪里?我怎样才能纠正它以获得所需的结果。