ComboBox has old value after Clear ()

I have two comboBox cb_Brand and cb_Model in winForm.

cb_Model populates the values ​​on the Select mark. The problem is this: if we select the any brand and select any model under this brand, cb_Model will not lose the value of the previous selected model. for example: if we select the Audi brand and the A3 model and select the Ford brand, when I click on cb_Model to select a model, it displays A3 as the selected model, but still the other models in the list belong to ford.

my code is:

 private void cb_Brand_SelectedIndexChanged(object sender, EventArgs e) { // Clear Current Data cb_Model.Text = ""; cb_Model.Items.Clear(); CarModel _carmodel = new CarModel (); // Get Selected Car Brnad int CarBrandID = _carmodel .GetCarBrandID(cb_Brand.Text); //Enable choice of Model SortedList<int, Model> colM; colM = Model.ReadModel(CarBrandID); cb_Model.DisplayMember = "ModelText"; foreach (Model objM in colM.Values) { cb_Model.Items.Add(objM); } } 

Any idea please .. Thanks


could not find the reason, but sorting out the temp fix:

 private void cb_Model_Click(object sender, EventArgs e) { cb_Model.Text = ""; } 

Thanks a lot guys greetings

+8
c # winforms combobox
source share
8 answers

Instead of adding items manually:

 foreach (Model objM in colM.Values) { cb_Model.Items.Add(objM); } 

Let .NET take care of this for you and replace it with this:

 cb_Model.DataSource = colMValues; 

which will attach the data to the list and automatically update the list items when the data source is installed.

You will also not need these lines:

 // Clear Current Data cb_Model.Text = ""; cb_Model.Items.Clear(); 

Read this for more information on binding lists (and other data sources) to ComboBoxes:

How to associate a Windows Forms ComboBox or ListBox component with data (MSDN)

+7
source share

@ w69rdy offers a great solution.

The reason cb_Model hasn't changed it, value is because you never changed the value. cb_Model.Items.Clear () does not change the selected index; only items are removed from the combo box.

Using the sample code provided in your question:

 // Clear Current Data cb_Model.Text = ""; cb_Model.Items.Clear(); cb_Model.SelectedIndex = -1; // would effectively clear the previously selected value. 
+6
source share

I had the same problem now and the Combobox ResetText method solved the problem for me

+3
source share

It will work

 combobox.ResetText(); 
+1
source share

I tried your example. For me, it worked as it should. You can try setting cb_model.SelectedText to "" or SelectedItem to null

0
source share

I found that maintaining the volume of the data source next to loading the combo box worked for me. I had data with a class level area, and this is not clear, but then I entered it in a function level area and made it clear after loading, and it worked.

0
source share

I have a similar problem, tried cmb.resettext, it clears the text, but not the value. In my upload form, I have the code below: Dim cmd As New SqlCommand("SELECT stud_id,name FROM student_details WHERE stud_id NOT IN (SELECT stud_id FROM student_details WHERE hostel_id!=0)", sqlcont.Conn) Dim dr As SqlDataReader = cmd.ExecuteReader Dim dat As New DataTable Dim j As Integer For j = 0 To dat.Rows.Count - 1 dr.Read() Next dat.Load(dr) cmbstud.DisplayMember = "name" cmbstud.ValueMember = "stud_id" cmbstud.DataSource = New BindingSource(dat, Nothing) dr.Close() In my btnhostel click event, I have the code below: frmallocateHostel_Load (nothing, nothing) I set this in an attempt to reload my dataset and thus my comboboxes.Using cmbstud.resettext just clears the text, not the value.

0
source share

I have the same problem as I used

 combobox1.SelectedIndex=-1 

and it works.

0
source share

All Articles