none
Clear Combobox RRS feed

  • Question

  • i have combobox value are loaded from one of two text file depending on what is selected in another combobox but when you change first combobox it loads the new combobox items under what in already is in there i want it clear what is already in there before loaded items from the text file

    i am using c# wpf and would the code for the cs document

    Tuesday, July 16, 2013 8:17 AM

Answers

  • Hi, Philip_95

    According to your description, if you have two combobox and there name are cb1 and cb2, you can use this code

     public MainWindow()
            {
                InitializeComponent();
               
                cb1.SelectionChanged += cb1_SelectionChanged;
            }

            void cb1_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {

               if(cb2.items.Count>0)

               {

                      cb2.Items.Clear();

                     //TODO Add new item to cb2

               }

            }

    The cb2.items.Clear() method will be clear all items in cb2, you also can use cb2.Items.Remove(..) method to remove one item.

    Thanks

    Best regards

    Tuesday, July 16, 2013 8:47 AM

All replies

  • combobox.items.clear()
    Tuesday, July 16, 2013 8:19 AM
  • this will clear all combobox items?
    Tuesday, July 16, 2013 8:21 AM
  • Hi, Philip_95

    According to your description, if you have two combobox and there name are cb1 and cb2, you can use this code

     public MainWindow()
            {
                InitializeComponent();
               
                cb1.SelectionChanged += cb1_SelectionChanged;
            }

            void cb1_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {

               if(cb2.items.Count>0)

               {

                      cb2.Items.Clear();

                     //TODO Add new item to cb2

               }

            }

    The cb2.items.Clear() method will be clear all items in cb2, you also can use cb2.Items.Remove(..) method to remove one item.

    Thanks

    Best regards

    Tuesday, July 16, 2013 8:47 AM