Event to detect an item added to a ComboBox

I am creating a custom control that inherits from ComboBox. I need to determine when an item has been 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 everything that I found on the Internet, including this thread , but the link in the response was disabled, and I did not guess what to do.

For example, this code in Vb.net:

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

It states that the Items property is not defined with WithEvents .

The control does not use a BindingSource. I need a control to perform a custom action when adding an element. Elements are added directly to the .Items property with:

 customComboBox.Items.Add("item"); 

Can this be done?

+1
source share
4 answers

I think the best approach would be to listen to ComboBox's native posts:

Do not be fooled by the word STRING, they all start every time you add, insert or delete an item. Therefore, 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 
+4
source

If your ComboBox supported by BindingSource , you can listen to the AddingItem event and handle it accordingly.

+1
source

You control when items are added to the ComboBox. Thus, events do not fire when this happens.

You are the one who adds items to the ComboBox. This is not an external executable that does this, this is your code. That way, you can guarantee that all your additions are done using the AddItem (item As Object) {...} function, which should handle the logic you need to do when items are added inside it. Thus, there is no need for events.

0
source

I recently struggled with the same issue and found that the documentation and other publications on the network are missing. At its core, Windows Forms ComboBox are two controls in one. Compact ListBox and TextBox. I wanted to determine when the user typed in a TextBox a new record that was not 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 does not define an event that directly covers this case, and the TextChanged event is too granular.

I found the following logic in a Leave event handler to detect a potential new item that is not in 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 just use string values ​​as an element of a list, then the newItem presented above is just cb.Text.

0
source

All Articles