2
votes

The title is somewhat misleading. I can reduce the .Items Count to zero by the usual methods, but the dropdown list area keeps its former dimensions.

  1. When adding items to ComboBox manually, I can do ComboBox.Items.Clear. The ComboBox.Items count is reduced to zero.

  2. When databinding ComboBox, I can do ComboBox.DataSource = Nothing. Or, set the BindingSource = Nothing, if using one. The ComboBox.Items Count is reduced to zero.

However, the combobox dropdown area retains the rows it had been populated with, except that they are "empty". In other words, it's a white box which is the same height as the list it had contained.

Seems to me that if I clear a ComboBox, it ought to appear identical to one which has never been bound or filled.

The DropDownStyle = DropDownList

ComboBox before filling/binding:

ComboBox before filling/binding

ComboBox after filling/binding:

ComboBox after filling/binding

ComboBox after clearing/setting datasource/bindingsource = Nothing:

ComboBox after clearing/setting datasource/bindingsource = Nothing

Does anyone know a way to prevent this? If I unbind or otherwise clear the ComboBox, I'd like the dropdown to then consist of one empty row, as in the first image.

Thanks.

3

3 Answers

2
votes

The following should fix that:

ComboBox.DropDownStyle = ComboBoxStyle.DropDown
ComboBox.DropDownStyle = ComboBoxStyle.DropDownList

For some reason changing the 'DropDownStyle' property of the combobox to something that is not DropDownList, and then changing it back resizes the DropDownList to the minimum possible height for its entries, or at least in Visual Studio 2015 with .NET Framework 4.5. Hope this helps.

Edit: It's strange that if the ComboBox's style is ComboBoxStyle.DropDown that ComboBox.DropDownHeight = 106 will change the height, but if the style is ComboBoxStyle.DropDownList it won't. Yet another mystery of .NET!

2
votes

To force reset a height of dropdown element you need to change ComboBox.DropDownStyle to some other and back to the original

Me.ComboBox1.DataSource = Nothing
Me.ComboBox1.DropDownStyle = ComboBoxStyle.DropDown
Me.ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
1
votes

You must set the DropDownHeight property of the ComboBox control after clearing the DataSource. Here is an example that sets it back to a default that you can adapt for your own purposes.

Private m_intDefaultDropDownHeight As Integer = 1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Dim lstItems As New List(Of String)

    lstItems.Add("A")
    lstItems.Add("B")
    lstItems.Add("C")

    m_intDefaultDropDownHeight = ComboBox1.DropDownHeight

    ComboBox1.DataSource = lstItems

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    ComboBox1.DataSource = Nothing
    ComboBox1.DropDownHeight = m_intDefaultDropDownHeight
End Sub