Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combo Box Size Issue After All Items Are Removed

My application contains a ComboBox that the user can delete items from. When the program starts up it populates the ComboBox from a list of strings read in from a 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 it's been populated:

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 a few items have been removed:

Combo Box Reduced

The problem I am having is when the last item is removed the ComboBox resizes itself to the size it was when it was initially populated. There aren't any items in the ComboBox but it sizes itself as if there were.

Here is a screenshot after all the items have been removed:

Combo Box Cleared

As you can see the size is too big. I would think that after all the items were cleared it would look like the following:

Combo Box Initial No Data

Any ideas as to why this is happening?

like image 964
Jan Tacci Avatar asked Jul 21 '13 04:07

Jan Tacci


People also ask

How do you resize a combo box?

When resizing a combo box through AS the dropdown stays at the same width as the first time it drops down. So user places combo box on the page, clicks the down arrow, sees the options, selects an option or clicks down arrow again to close, resizes the width of the drop down, clicks the down arrow.

How do I resize a ComboBox in Visual Studio?

Simply Change the Font Size of the ComboBox in the property window that will automatically increase the Height of your ComboBox. Thanks.

How many items can be added to a ComboBox?

The ComboBox can easily contain thousands of items. The 100 limit you are referring to is the visible portion which is displayed when the ComboBox drop down appears. Note that the performance is tied to what type of data is being populated within the ComboBox ; a complex object versus a simple string value.


1 Answers

Try to use this at the end of your code when you are filling the combobox items:

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

Then to clear it up:

comboBoxNumTreno.ResetText();
comboBoxNumTreno.Items.Clear();
comboBoxNumTreno.SelectedIndex = -1;
comboBoxNumTreno.DropDownHeight = 106; // default value
comboBoxNumTreno.IntegralHeight = false; 
like image 105
Luke Avatar answered Sep 17 '22 16:09

Luke