Event to detect item added to ComboBox

后端 未结 4 1719
广开言路
广开言路 2021-01-23 17:37

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.

相关标签:
4条回答
  • 2021-01-23 18:09

    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.

    0 讨论(0)
  • 2021-01-23 18:13

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

    0 讨论(0)
  • 2021-01-23 18:20

    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.

    0 讨论(0)
  • 2021-01-23 18:27

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

    • CB_ADDSTRING
    • CB_INSERTSTRING
    • CB_DELETESTRING
    • CB_RESETCONTENT

    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
    
    0 讨论(0)
提交回复
热议问题