שתף באמצעות


hide an item in a combo box

Question

Thursday, July 24, 2008 6:24 AM

 

Hi,
 I have a problem and I am new to this, so need some help. We have a combo box with 5 items(legacy code). We also have code at multiple places as follows:

         switch (colorComboBox.SelectedIndex)
            {
                case 0:
                    session = 8;
                    break;
                case 1:
                    session = 15;
                    break;
                case 2:
                    session = 16;
                    break;
                case 3:
                    session = 24;
                    break;
                default:
                    session= 32;
                    break;
            }

The requirement is to remove the first item of this comboBox, without (hopefully) changing all the case statements which rely on the selectedIndex. Is there a way to keep the item in the comboBox, but NOT let it be visible to the user, OR not let it be selected, so that when the user selects the 1st item on the list he sees, the selectionIndex still remains 1 and not 0.

This may be really trivial, but any help would be appreciated:)

All replies (1)

Wednesday, July 30, 2008 12:42 PM ✅Answered

dp2k8 said:

The requirement is to remove the first item of this comboBox, without (hopefully) changing all the case statements which rely on the selectedIndex. Is there a way to keep the item in the comboBox, but NOT let it be visible to the user, OR not let it be selected, so that when the user selects the 1st item on the list he sees, the selectionIndex still remains 1 and not 0.

Hi dp2k8,

  1. You cannot hide items in ComboBox control.
        However, you can remove item like this: 
            ComboBox1.Items.Remove(Value)
            ComboBox1.Items.RemoveAt(index)
        You can add item back like this:
            ComboBox1.Items.Add(Value)
            ComboBox1.Items.Insert(Index, Value)

2. It would be better to check items using the selectedValue or SelectedText instead of the selectedIndex.

3. You can make the items in the combo box sorted like this:
     ComboBox1.Sorted = True

Best regards,
Martin Xie