none
How to set combobox to no selected item RRS feed

  • Question

  •  

    Hello

     

    i have an application VS.NET 2008 CF 2.0 WM 5

     

    i fill combobox with collection of objects and want to set no utem selected in combo

    Sunday, July 6, 2008 9:20 AM

Answers

  •  

    Hi,

     

    If you are not binding your combobox  to a datasource then you can just use the combobox.insert() method where you can pass 0th index as param to show blank on top.

     

    obj.id=0;

    obj.Text="";

    combobox.insert(0,obj);

     

    OR

     

     

    If you are binding your combobox then see example below:

    List<PersonStruct> NameList = new List<PersonStruct>();

     

    cbName.Items.Clear();

    PersonStruct xSelect = new PersonStruct();

    xSelect.ID = 0;

    xSelect.Name = "";

    NameList = RetrieveNames();  //Returns a generic based on a structure populate from a dataset

     

    // Compact Edition limitation - cannot insert to the Combobox, you must insert into the datasource

    NameList.Insert(0, xSelect); //<-----------------------------------------------------------------

     

    // Guard clause for data set passed in

    if (NameList != null)

    {

    cbName.DisplayMember = "Name";

    cbName.ValueMember = "ID";

    cbName.DataSource = NameList;

    }

     

     

     

    Sunday, July 6, 2008 12:46 PM

All replies

  •  

    Hi,

     

    If you are not binding your combobox  to a datasource then you can just use the combobox.insert() method where you can pass 0th index as param to show blank on top.

     

    obj.id=0;

    obj.Text="";

    combobox.insert(0,obj);

     

    OR

     

     

    If you are binding your combobox then see example below:

    List<PersonStruct> NameList = new List<PersonStruct>();

     

    cbName.Items.Clear();

    PersonStruct xSelect = new PersonStruct();

    xSelect.ID = 0;

    xSelect.Name = "";

    NameList = RetrieveNames();  //Returns a generic based on a structure populate from a dataset

     

    // Compact Edition limitation - cannot insert to the Combobox, you must insert into the datasource

    NameList.Insert(0, xSelect); //<-----------------------------------------------------------------

     

    // Guard clause for data set passed in

    if (NameList != null)

    {

    cbName.DisplayMember = "Name";

    cbName.ValueMember = "ID";

    cbName.DataSource = NameList;

    }

     

     

     

    Sunday, July 6, 2008 12:46 PM
  • You may also try this:

         comboBox1.SelectedItem = null;

    Tuesday, July 8, 2008 1:32 AM
    Moderator
  • I researched the same question and came up with the following.  According to the MSDN page listed below, a ComboBox.SelectedIndex range is   = -1 for no  selection, and  0 and above for the selected item index starting with zero.

    https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.combobox.selectedindex?view=netframework-4.7.2

    So

    ComboBox.SelectedIndex   =   -1;                                              //  will work :-)

    Monday, September 24, 2018 6:13 AM