从另一个表单的组合框中获取值

时间:2017-11-28 10:43:05

标签: c# winforms combobox

我无法从form2组合框中获得价值。我检查了组合框修改器设置为“公共”。

我得到'对象没有引用对象的实例'错误。这是什么错误?

//Form1
private DialogIO dio;

private void uosIO_ValueChanged(object sender, EventArgs e)
{
   dio = new DialogIO();
   dio.Show(); // Open Form2 and Set Some value on Combobox
}


// BGWorker get run call from form2 and run it on form1.
private void bgwCustomIO_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
   MessageBox.Show(dio.uceMinHour.Text); // Show Combobox Value
}

1 个答案:

答案 0 :(得分:0)

考虑检查null也是......

 private void bgwCustomIO_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
    {
        if (dio == null || string.IsNullOrEmpty(dio.uceMinHour.Text))
        {
            return;
        }
       MessageBox.Show(dio.uceMinHour.Text); // Show Combobox Value
    }