2017-06-05 45 views
2
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錯誤出現。我該怎麼辦?正在獲取comboBox1.SelectedValue

回答

1

也許你正在尋找SelectedItem而不是SelectedValue

錯誤object reference not set to an instance of an object意味着SelectedValuenull

+0

所以是這樣的話,太感謝你了:) –

相關問題