1

我有这种情况,它在将数据加载到组合框和文本框后有效,但是当应用程序首次加载并且组合框为 = 烟草使用?并尝试将文本框设置为空我得到“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;
  }
}
4

3 回答 3

2

您确定应用程序在第一种情况下输入,并设置了 strTobaccoCode 的值吗?设置断点并检查。

使用“默认”选项使您的代码更安全一些:

private void cbTobacco_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (cbTobacco.Text != null)
    {
        string strTobaccoCode;
        switch (Convert.ToString(cbTobacco.Text))
        {
            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;
            default: strTobaccoCode = ""; break;
        }
        cbTobaccoCode.Text = strTobaccoCode;
    }
}
于 2013-10-07T14:51:54.953 回答
1

您还应该设置默认情况。另外,您为什么要直接访问 cbTobacco?您应该将发件人(如果您已正确连接您的事件,则为 cbTobacco)到一个文本框并从那里访问值:

 private void cbTobacco_SelectionChanged(object sender, SelectionChangedEventArgs e)
 {
   var textBox = sender as TextBox;

   if (textBox != null && textBox.Text != null)
   {
     switch (Convert.ToString(textBox.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;
        default: strTobaccoCode = ""; break;
      }

      textBox.Text = strTobaccoCode ?? "";
  }
}
于 2013-10-07T14:53:33.783 回答
0

尝试添加这个(验证文本框是否为空,然后验证文本是否为空)

    if(cbTobacco != null)

前线

if (cbTobacco.Text != null)
于 2013-10-07T17:17:04.150 回答