我有这种情况,它在将数据加载到组合框和文本框后有效,但是当应用程序首次加载并且组合框为 = 烟草使用?并尝试将文本框设置为空我得到“NullReferenceException - 对象引用未设置为对象的实例”。不知道如何解决这个问题,并且能够在选择“烟草使用?”时保持它清除文本框。还有“烟草使用?” 是组合框上的默认值。
组合框 cbTobacco.Text
文本框 = cbTobaccoCode.Text
private void cbTobacco_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (cbTobacco.Text != null)
{
switch (Convert.ToString(cbTobacco.Text))
{
case "Tobacco Use?": strTobaccoCode = ""; break;
case "1 - Current every day smoker": strTobaccoCode = "449868002"; break;
case "2 - Current some day smoker": strTobaccoCode = "428041000124106"; break;
case "3 - Former smoker": strTobaccoCode = "8517006"; break;
case "4 - Never smoker": strTobaccoCode = "266919005"; break;
case "5 - Smoker, current status unknown": strTobaccoCode = "77176002"; break;
case "6 - Unknown if ever smoked": strTobaccoCode = "266927001"; break;
case "7 - Heavy tobacco smoker": strTobaccoCode = "428071000124103"; break;
case "8 - Light tobacco smoker": strTobaccoCode = "428061000124105"; break;
}
cbTobaccoCode.Text = strTobaccoCode;
}
}