0

When try to access newly added value in the combo box it gives this error message!

Object reference not set to an instance of an object

window have one combo box and text box! when click the Add new item button, text box value should be loaded in to the combo box.! This loaded action working fine! but when we try to access the newly added one( for example when click edit button) , above error message displayed.

XAML Code:

<ComboBox x:Name="cmbList"  x:FieldModifier="public" HorizontalAlignment="Left" Margin="58,10,0,0" VerticalAlignment="Top" Width="147" 
                    <ComboBoxItem Content="1st Item"/>
                    <ComboBoxItem Content="2nd Item"/>
                    <ComboBoxItem Content="3rd Item"/>
 </ComboBox>

Button Click function :

cmbList.Items.Add(textbox1.text)

Up to this point it working properly! If I try to re select it gives this error message!

Please anyone correct me!

Thanks in Advance!

kAsdh
  • 356
  • 1
  • 10
  • 25
  • could you please post the entire button click event code? and the part where you try to access the new item. this is the part that throws the exception, am I right? – Mong Zhu Aug 11 '16 at 10:44
  • private void btnAdd_Click(object sender, RoutedEventArgs e) { cmbList.Items.Add(textbox1.text) } In the case, select newly added item from combo box , it display but before display it fives this error message! – kAsdh Aug 11 '16 at 10:51

1 Answers1

0

You are adding a String to a collection of ComboBoxItems and therefore the ComboBox or your editing code won't be happy as I imagine you are expecting a ComboBoxItem.

Try replacing this line in your click event:

cmbList.Items.Add(textbox1.text)

With this:

cmbList.Items.Add(new ComboBoxItem { Content = textbox1.text });
Bijington
  • 3,661
  • 5
  • 35
  • 52