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

时间:2014-07-15 15:01:13

标签: c# .net vb.net winforms combobox

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

我尝试了我在互联网上找到的所有内容,包括this thread,但答案中的链接是离线的,我无法猜测我该怎么做。

例如,这个代码在Vb.net中:

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

它表示Items属性未定义WithEvents

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

customComboBox.Items.Add("item");

可以吗?

4 个答案:

答案 0 :(得分:4)

我认为最好的方法是听取原生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

答案 1 :(得分:1)

如果您的ComboBoxBindingSource支持,那么您可以收听AddingItem事件并相应地处理。

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

我最近在同一个问题上苦苦挣扎,发现缺少文档和其他Web帖子。 Windows Forms ComboBox的核心是两个控件合而为一。紧凑的ListBox和TextBox。我想检测用户何时在TextBox中键入了一个不包含在Items集合中的新条目,以便可以处理该新条目并将其添加到要选择的条目列表中。

该控件未定义直接覆盖这种情况的事件,而TextChanged事件过于精细。

我发现,Leave事件处理程序中的以下逻辑可以检测出不在列表中的潜在新项目。

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;
    }

}

如果只是将字符串值用作列表项,则上面的newItem就是cb.Text。

相关问题