获取comboBox1.SelectedValue

时间:2017-06-05 00:04:08

标签: c# visual-studio

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        switchItem();
    }
private void switchItem()
        {
            var content = richTextBox1;
            switch (comboBox1.SelectedValue.ToString())
            {
                case "Fruit":
                    content.Text = "Apple";
                    break;
                default:
                    content.Rtf = "Not Selected";
                    break;
            }
        }

正如您所看到的,我正在尝试获取comboBox1的SelectedValue,并显示此object reference not set to an instance of an object错误。我该怎么办?

1 个答案:

答案 0 :(得分:1)

也许您正在寻找SelectedItem而不是SelectedValue

错误object reference not set to an instance of an object表示SelectedValuenull