0

我已经设法过滤我的组合框,以便它过滤并显示正确的记录,即当键入 A 时显示所有 A 记录,键入 B 时显示所有 B 记录,依此类推。但是,当在组合框中找不到记录时,是否可以显示一个消息框?

我到目前为止的编码是:-

  Private Sub cmblogged_KeyPress(sender As Object, e As KeyPressEventArgs) Handles cmblogged.KeyPress
        If Char.IsControl(e.KeyChar) Then Return

        With Me.cmblogged

            Dim ToFind As String = .Text.Substring(0, .SelectionStart) & e.KeyChar
            Dim Index As Integer = .FindStringExact(ToFind)

            If Index = -1 Then Index = .FindString(ToFind)
            If Index = -1 Then Return

            .SelectedIndex = Index
            .SelectionStart = ToFind.Length
            .SelectionLength = .Text.Length - .SelectionStart

            e.Handled = True

        End With

    End Sub
4

1 回答 1

1

用你的代码实现你想要的很简单,只需转换If Index = -1 Then Return为:

 If Index = -1 Then
     MessageBox.Show("Not Found.")
     Return
 End If

ComboBox在任何情况下,请注意在执行您的代码现在执行的相同操作时有一个内置功能:AutoCompleteMode不同于NoneAutoCompleteSource设置为ListItems. 从逻辑上讲,您可以改进您的代码以执行更复杂的操作(我猜就是这种情况),但我更愿意强调这个问题以防万一。

于 2013-08-02T16:45:41.480 回答