SysDragon
SysDragon

Reputation: 9888

Event to detect item added to ComboBox

I am creating a custom control that inherits from ComboBox. I need to detect when a Item is added to the ComboBox to perform my own checks. It doesn't matter if it's C# or Vb.NET, but I don't know how to do it.

I tried all the things I found across the internet, including this thread, but the link in the answer is offline and I didn't manage to guess what should I do.

For example, this code in Vb.net:

Public Sub SomeFunc() Handles Items.CollectionChanged
    '....
End Sub

It says that Items property is not defined WithEvents.

The control is not using a BindingSource. I need the control to perform a custom action when an Item is added. Items are added directly into the .Items property with:

customComboBox.Items.Add("item");

Can it be done?

Upvotes: 1

Views: 2063

Answers (4)

Kenneth W Stoll
Kenneth W Stoll

Reputation: 1

I was recently struggling with the same question and found the documentation and other web posts lacking. At its heart a Windows Forms ComboBox is two controls in one. A compact ListBox and a TextBox. I wanted to detect when a user had typed a new entry into the TextBox that wasn't contained in the Items collection so that the new item could be processed and possibly added to the list of items to be selected.

The control doesn't define an event covering this case directly and the TextChanged event is far too granular.

What I found was the following logic in a Leave event handler to detect a potential new item that isn't on the list.

void cb_Leave(object sender, EventArgs e) {
    if (cb.SelectedIndex < 0 && string.IsNullOrEmpty(cb.Text)) {
        // The Text represents the potential new item provided by the user
        // Insert validation, value generation, etc. here
        // If the proposed text becomes a new item, add it to the list
        ListItemType newItem = new ListItemType(cb.Text);
        cb.Items.Add(newItem);

        // And don't forget to select the new item so that the
        // SelectedIndex and SelectedItem are updated to reflect the addition
        cb.SelectedItem = newItem;
    }

}

If you are simply using string values as your list item, then the newItem above is simply the cb.Text.

Upvotes: 0

I think the best approach would be to listen for the native ComboBox messages:

Don't be fooled by the word STRING, they are all fired whenever you add, insert or delete an item. So when the list is cleared.

Public Class UIComboBox
    Inherits ComboBox

    Private Sub NotifyAdded(index As Integer)
    End Sub

    Private Sub NotifyCleared()
    End Sub

    Private Sub NotifyInserted(index As Integer)
    End Sub

    Private Sub NotifyRemoved(index As Integer)
    End Sub

    Protected Overrides Sub WndProc(ByRef m As Message)
        Select Case m.Msg
            Case CB_ADDSTRING
                MyBase.WndProc(m)
                Dim index As Integer = (Me.Items.Count - 1)
                Me.NotifyAdded(index)
                Exit Select
            Case CB_DELETESTRING
                MyBase.WndProc(m)
                Dim index As Integer = m.WParam.ToInt32()
                Me.NotifyRemoved(index)
                Exit Select
            Case CB_INSERTSTRING
                MyBase.WndProc(m)
                Dim index As Integer = m.WParam.ToInt32()
                Me.NotifyAdded(If((index > -1), index, (Me.Items.Count - 1)))
                Exit Select
            Case CB_RESETCONTENT
                MyBase.WndProc(m)
                Me.NotifyCleared()
                Exit Select
            Case Else
                MyBase.WndProc(m)
                Exit Select
        End Select
    End Sub

    Private Const CB_ADDSTRING As Integer = &H143
    Private Const CB_DELETESTRING As Integer = &H144
    Private Const CB_INSERTSTRING As Integer = 330
    Private Const CB_RESETCONTENT As Integer = &H14B

End Class

Upvotes: 6

Hesham Eraqi
Hesham Eraqi

Reputation: 2542

You are in control of when items are added to a ComboBox. So, there are no events fired when this happens.

You are the one who adds items to the ComboBox. It's not an external executable that do this, it's your code. So, you can ensure that all your adds are done via a function AddItem(item As Object) {...} that you should handle the logic you need to do when items are being added inside it. So, no need for events.

Upvotes: 0

Chris Hinton
Chris Hinton

Reputation: 866

If your ComboBox is backed by a BindingSource, then you could listen for the AddingItem event and handle it accordingly.

Upvotes: 1

Related Questions