我创建一个自定义的控件,继承自组合框。我要当一个项目被添加到组合框来执行自己的检查,以检测。不要紧,如果是C#或Vb.NET,但我不知道该怎么做。
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.
例如,该code在Vb.net:
For example, this code in Vb.net:
Public Sub SomeFunc() Handles Items.CollectionChanged
'....
End Sub
它说,产品
属性没有被定义的 WithEvents就
。
It says that Items
property is not defined WithEvents
.
控制没有使用的BindingSource。我需要控制时,该产品被添加到执行自定义操作。项目直接添加到 .Items
财产:
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?
我认为最好的办法是将监听本机的 ComboBox的邮件:
I think the best approach would be to listen for the native ComboBox messages:
CB_ADDSTRING CB_INSERTSTRING CB_DELETESTRING CB_RESETCONTENT CB_ADDSTRING CB_INSERTSTRING CB_DELETESTRING CB_RESETCONTENT不要被这个词所迷惑的 STRING 的,他们都当添加,插入或删除项激活。因此,当列表被清除。
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