0
votes

I have a combobox with AutoCompleteMode = SuggestAppend and AutoCompleteSource=ListItems.

In SuggestAppend, when the user input characters, the listbox control is being filtered automatically.

I would like to get the number of items in the listbox during user input. At the beginning the listbox is fully populated.

I tried the following but it always returns the number of items in the combo and not the number of filtered items in the listbox

int count = ItemsComboBox.Items.Count.ToString();
2

2 Answers

0
votes

I'm not 100% sure about what you want to do, but as I understand, the goal is to enable a button automatically.

The ComboBox class has events called ControlAdded and ControlRemoved, which occur when a Control is added or removed from the ComboBox.Items. So you can check the value of comboBox.Items.Count after every control add/remove.

private void comboBox1_ControlRemoved(object sender, ControlEventArgs e)
        {
                if (comboBox1.Items.Count == 0) button1.Enabled = false;
        }

And of course you need to handle both the ControlAdded and Control Removed events if you not only want to check about 0. You can do this with double clicking next to the name of the event int he Properties Windows of the ComboBox in Visual Studio which will add teh following line to your Designer file:

this.comboBox1.ControlRemoved += new System.Windows.Forms.ControlEventHandler(this.comboBox1_ControlRemoved);

You can use the same void for both events (it depends on your project of course).

0
votes

This worked out for me but I changed AutoCompleteMode = SuggestAppend to AutoCompleteMode = Suggest

Public Class Form2

  Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Me.ComboBox1.Text = ""
    Me.ComboBox1.Items.Add("a")
    Me.ComboBox1.Items.Add("aaa")
    Me.ComboBox1.Items.Add("combo")
    Me.ComboBox1.Items.Add("combobox")
    Me.ComboBox1.Items.Add("combobox test")
    Me.ComboBox1.Items.Add("common")
    Me.ComboBox1.Items.Add("common dialog")
  End Sub

  Private Sub ComboBox1_TextChanged(sender As Object, e As System.EventArgs) Handles ComboBox1.TextChanged
    Dim count As Integer = 0

    For Each op As String In ComboBox1.Items
      If (op Is Nothing OrElse op.Length < ComboBox1.Text.Length) Then
        Continue For
      End If
      If (ComboBox1.Text = op.Substring(0, ComboBox1.Text.Length)) Then
        count += 1
      End If
    Next

    Label1.Text = count
  End Sub

End Class