0

I am trying to change the values in ListBox 2 based on the item selected in ListBox 1.

public partial class MainWindow : Window
    {
        
        public MainWindow()
        {
            InitializeComponent();            

            ListBox2.Items.Clear();
            ListBox2.Items.Add("Monday"); 
 

        }

        private void ListBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {            

            switch (ListBox1.SelectedIndex)
            {

                case 0:
                    ListBox2.Items.Clear();
                    ListBox2.Items.Add("Monday");
                    break;

                case 1:
                    ListBox2.Items.Clear();
                    ListBox2.Items.Add("Tuesday");
                    break;

            }            

        }
    }

This works if I remove case 0. ListBox2 loads with "Monday" and changes to "Tuesday" when I select index 1 in ListBox1. It remains "Tuesday" even if I select index 0. And gives following error with the above code when I run it:

NullReferenceException: Object reference not set to an instance of an object.

0 Answers0