gpt4 book ai didi

c# - 检测添加到 ComboBox 的项目的事件

转载 作者:太空宇宙 更新时间:2023-11-03 18:29:52 25 4
gpt4 key购买 nike

我正在创建一个继承自 ComboBox 的自定义控件。我需要检测何时将项目添加到 ComboBox 以执行我自己的检查。是C#还是Vb.NET都无所谓,就是不知道怎么做。

我尝试了我在互联网上找到的所有东西,包括 this thread ,但答案中的链接已离线,我没能猜到我该怎么做。

例如,Vb.net中的这段代码:

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

它表示 Items 属性未定义 WithEvents

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

customComboBox.Items.Add("item");

可以吗?

最佳答案

我认为最好的方法是监听本地 ComboBox messages :

不要被 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

关于c# - 检测添加到 ComboBox 的项目的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24761600/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com