用于检测添加到ComboBox的项目的事件

时间:2021-05-01 00:05:52

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.

我正在创建一个继承自ComboBox的自定义控件。我需要检测何时将一个Item添加到ComboBox以执行我自己的检查。它是C#还是Vb.NET并不重要,但我不知道该怎么做。

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:

例如,Vb.net中的此代码:

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

It says that Items property is not defined WithEvents.

它表示Items属性未定义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:

该控件未使用BindingSource。我需要控件在添加项目时执行自定义操作。项目直接添加到.Items属性中:

customComboBox.Items.Add("item");

Can it be done?

可以吗?

3 个解决方案

#1


3  

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

我认为最好的方法是监听本机ComboBox消息:

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.

不要被STRING这个词所迷惑,每当你添加,插入或删除一个项目时都会被解雇。所以当清单清除时。

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

#2


1  

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

如果您的ComboBox由BindingSource支持,那么您可以侦听AddingItem事件并相应地处理它。

#3


0  

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

您可以控制何时将项目添加到ComboBox。因此,当发生这种情况时,没有事件被触发。

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.

您是向ComboBox添加项目的人。执行此操作不是外部可执行文件,而是您的代码。因此,您可以确保所有添加都是通过函数AddItem(item As Object){...}来完成的,您应该处理在项目中添加项目时需要执行的逻辑。所以,不需要事件。

#1


3  

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

我认为最好的方法是监听本机ComboBox消息:

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.

不要被STRING这个词所迷惑,每当你添加,插入或删除一个项目时都会被解雇。所以当清单清除时。

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

#2


1  

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

如果您的ComboBox由BindingSource支持,那么您可以侦听AddingItem事件并相应地处理它。

#3


0  

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

您可以控制何时将项目添加到ComboBox。因此,当发生这种情况时,没有事件被触发。

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.

您是向ComboBox添加项目的人。执行此操作不是外部可执行文件,而是您的代码。因此,您可以确保所有添加都是通过函数AddItem(item As Object){...}来完成的,您应该处理在项目中添加项目时需要执行的逻辑。所以,不需要事件。