C# 从 winform 应用程序中的 Combobox 读取值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10031603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 12:11:50  来源:igfitidea点击:

Read value from Combobox in winform application

c#winformsnullreferenceexception

提问by kombo

I am building a Windows forms application using VS2010. I want to read the value the user selected from the dropdown ComboBoxand generate patientNobased on that. But when i run the application I get a NullReferenceException.

我正在使用 VS2010 构建 Windows 窗体应用程序。我想读取用户从下拉列表中选择的值ComboBoxpatientNo基于该值生成。但是当我运行应用程序时,我得到一个NullReferenceException.

Here is my code:

这是我的代码:

private void button1_Click(object sender, EventArgs e)
{
    string patientNumber;

    string gender = comboBox2.SelectedValue.ToString();
    if (gender != null)
    {
        if (gender == "Female")
        {
            var generator = new PatientNumberGenerator();
            patientNumber = generator.GeneratePatientNumber(Gender.Female);
            const string message = "patientNumber";

            const string caption = "Testing PatientNumber class";
            var result = MessageBox.Show(message, caption,
                                         MessageBoxButtons.YesNo,
                                         MessageBoxIcon.Question);

        }
        else if (gender == "Male")
        {
            var generator = new PatientNumberGenerator();
            patientNumber = generator.GeneratePatientNumber(Gender.Male);
            const string message = "patientNumber";

            const string caption = "Testing PatientNumber class";
            var result = MessageBox.Show(message, caption,
                                         MessageBoxButtons.YesNo,
                                         MessageBoxIcon.Question);
        }
   }

}

}

采纳答案by raveturned

You haven't said which line the exception is occuring on, so it's a bit of a shot in the dark. However:

你还没有说异常发生在哪一行,所以这有点摸不着头脑。然而:

string gender = comboBox2.SelectedValue.ToString();

comboBox2.SelectedValuemight be null, in which case trying to call ToString()on it would cause a NullReferenceException. Try:

comboBox2.SelectedValue可能为 null,在这种情况下尝试调用ToString()它会导致 NullReferenceException。尝试:

if (comboBox2.SelectedValue == null)
{
    return;
}
string gender = comboBox2.SelectedValue.ToString();
//[etc…]

If that's not your issue, the exception may be inside other calls in that method, such as GeneratePatientNumber.

如果这不是您的问题,则异常可能在该方法的其他调用中,例如GeneratePatientNumber.

回答by Ash Burlaczenko

The only place I can see you would get a NullReferenceException other than in GeneratePatientNumber is

除了 GeneratePatientNumber 之外,我唯一能看到你会得到 NullReferenceException 的地方是

comboBox2.SelectedValue.ToString();

If nothing is selected .SelectedValuewill be null therefore cannot have ToString()called on it.

如果没有选择任何内容.SelectedValue将为 null 因此不能ToString()调用它。

回答by Brad Rem

If somewhere in your code you are setting comboBox2.DataSourceproperty to some datasource, then .SelectedValuewill retrieve a value. If you are notsetting the .DataSourceproperty, then SelectedValuewill return null.

如果您在代码中的某处将comboBox2.DataSource属性设置为某个数据源,.SelectedValue则将检索一个值。如果您没有设置该.DataSource属性,SelectedValue则将返回 null。

I assume that since you keep getting null values you are not setting a datasource but instead setting your Items in the VS Designer. If that's the case, you should instead be using:

我假设由于您不断获得空值,因此您不是在设置数据源,而是在 VS 设计器中设置您的项目。如果是这种情况,您应该使用:

comboBox2.SelectedItem.ToString()

to retrieve the SelectedItem in your list.

检索列表中的 SelectedItem。

回答by campagnolo_1

I know this is over a year old but I was working on a similar project with the exact same error. What fixed it for me was using

我知道这已经一年多了,但我正在做一个类似的项目,但错误完全相同。为我修复它的是使用

comboBox2.SelectedText.ToString();

or

或者

comboBox2.SelectedItem.ToString();

回答by Melwyn Castelino

Don't use combobox1.SelectedValuein caseuse:

不要combobox1.SelectedValuecase使用中使用:

string selected = combobox1.SelectedItem.ToString();

It worked for me.

它对我有用。