Combined block size error after deleting all elements

My application contains a ComboBox from which the user can delete items. When the program starts, it populates the ComboBox from the list of lines read from the configuration file.

Here is the code to add items:

// version list is an array of strings foreach (string version in versionList) { versionComboBox.Items.Add(version); } if (versionComboBox.Items.Count > 0) { versionComboBox.SelectedIndex = 0; } 

Here is a screenshot of the combo box after filling it out:

Combo Box Initial

If the user clicks the Delete button, the program removes the selected item from the ComboBox using the following code:

 if (versionComboBox.SelectedIndex >= 0) { versionComboBox.Items.Remove(versionComboBox.SelectedItem); } if (versionComboBox.Items.Count > 0) { versionComboBox.SelectedIndex = 0; } 

Here is a screenshot of the combo box after removing several items:

Combo Box Reduced

The problem I ran into is when the last item is deleted, the ComboBox resizes to the size when it was originally populated. There are no elements in the ComboBox, but it measures as if they were.

Here is a screenshot after deleting all the elements:

Combo box cleared

As you can see, the size is too large. I think that after all the elements have been cleared, it will look like this:

Combo Box Initial No Data

Any ideas as to why this is happening?

+4
source share
4 answers

To clear the combo box, you can add the following:

 if (versionComboBox.Items.Count == 0) { versionComboBox.Text = string.Empty; versionComboBox.Items.Clear(); versionComboBox.SelectedIndex = -1; } 

Another approach is to manipulate the elements in the data source and reinstall the control each time (much less than you can worry about).

+1
source

Try using this at the end of your code when you populate the elements of a list with a list:

 comboBoxNumTreno.IntegralHeight = true; // auto fit dropdown list 

Then to clear it:

 comboBoxNumTreno.ResetText(); comboBoxNumTreno.Items.Clear(); comboBoxNumTreno.SelectedIndex = -1; comboBoxNumTreno.DropDownHeight = 106; // default value comboBoxNumTreno.IntegralHeight = false; 
+3
source

I know this is an old post, but it took me a long time to figure it out, and I wanted someone to know in the future. After you clear the combo box, just make an empty addition and it resets the height.

 comboBox1.Items.Clear(); comboBox1.Items.Add(""); 
+2
source

set the DropDownHeight property to fix the size

  versionComboBox.DropDownHeight = 106; // default value 
0
source

Source: https://habr.com/ru/post/1492538/


All Articles