none
searchable combobox? RRS feed

  • Question

  •  

    how can i make the combobox searchable, i mean just like a textbox which will pick from the combo list? but must only select so it should not allow to type new values?


    Jassim Rahma
    Thursday, September 4, 2008 9:27 AM

Answers

  • You can do something like this:

    this.comboBox1.DisplayMember = "c2";
    this.comboBox1.ValueMember = "c1";
    this.comboBox1.DataSource = dt;

    this.comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
    this.comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;



    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by Zhi-Xin Ye Thursday, September 11, 2008 2:45 AM
    Tuesday, September 9, 2008 9:39 AM

All replies

  • Set the type of the combobox to DropDownList.  This prevents the user from selecting anything other than what is in the drop down list.

    Michael Taylor - 9/4/08
    http://p3net.mvps.org
    Thursday, September 4, 2008 1:48 PM
    Moderator
  • but you can't type to search?
    Jassim Rahma
    Thursday, September 4, 2008 4:59 PM
  • I don't follow.  As you type the appropriate element from the combo will be selected.  If you don't want the user to be able to select anything other than what is in the list then you can't give them the option to do so.  For example given the following list:

    Apples
    Oranges
    Peaches
    Pineapples

    If the user types P then you want to jump to Peaches.  However P is not valid (it isn't in the list) so it must select Peaches.  If the user then types Pi then it would jump to Pineapples because that is the only valid item in the list.

    If you want auto-completion style support then you can add it by setting up the auto completion properties on the control.  You'd need to assign the completion list to the control along with the mode (search, search/append, etc).  I'm not sure how this would work with the drop down list though.  Worse comes to worse you'd use a regular drop down combo and then handle the event for the selection changing to ensure the item is valid. 

    Michael Taylor - 9/4/08
    http://p3net.mvps.org

    Thursday, September 4, 2008 5:25 PM
    Moderator
    • Edited by Jassim Rahma Thursday, September 4, 2008 6:07 PM correct the link
    Thursday, September 4, 2008 6:04 PM
  • Then you want the autocompletion options.  The properties of interest are AutoCompleteSource and AutoCompleteMode.  You'll likely have to do a custom source so you'll need to set the AutoCompleteCustomSource to the list of items in the combo box.  This will give you what you want.

    Michael Taylor - 9/4/08
    http://p3net.mvps.org
    Thursday, September 4, 2008 6:31 PM
    Moderator
  • but i am using this code to keep the Displaymember & ValueMember..

    how can I integrate that into the AutoComplete?

    private void get_vehicle_branches()

    {

    try

    {

    sql_connection = new SqlConnection("Data Source=.\\SQLEXPRESS;initial catalog=rent_a_car_company;integrated security=true;");

    sql_connection.Open();

    sql_command = new SqlCommand("sp_get_vehicle_branches", sql_connection);

    sql_command.CommandType = CommandType.StoredProcedure;

    // Create data adapter object

    sql_adapter = new SqlDataAdapter();

    sql_adapter.SelectCommand = sql_command;

    // Create a dataset object and fill with data using data adapter's Fill method

    data_set = new DataSet();

    sql_adapter.Fill(data_set, "vehicle_branches");

    // Attach dataset's DefaultView to the combobox

    cboVehicleBranch.DisplayMember = "branch_name";

    cboVehicleBranch.ValueMember = "branch_id";

    cboVehicleBranch.DataSource = data_set.Tables["vehicle_branches"].DefaultView;

    cboVehicleBranch.SelectedIndex = -1;

    }

    // catch (Exception oE)

    // {

    // MessageBox.Show("Problem Populating Reader Box: [" + oE.ToString() + "]");

    // }

    finally

    {

    if (sql_reader != null) sql_reader.Close();

    // if (sql_connection != null)

    // {

    // if (sql_connection.State == ConnectionState.Open)

    // sql_connection.Close();

    // }

    }

    }


    Jassim Rahma
    Thursday, September 4, 2008 8:12 PM
  • You can do something like this:

    this.comboBox1.DisplayMember = "c2";
    this.comboBox1.ValueMember = "c1";
    this.comboBox1.DataSource = dt;

    this.comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
    this.comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;



    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by Zhi-Xin Ye Thursday, September 11, 2008 2:45 AM
    Tuesday, September 9, 2008 9:39 AM