vb.net 如何过滤我的组合框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18021493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to filter my combobox
提问by HymanSparrow
I have managed to filter my combobox so it filters and displays the correct records ie when A is typed all A records show, B is typed all B records show and so on. However it would it be possible to have a message box to display when no records is found in the combobox?
我设法过滤了我的组合框,以便它过滤并显示正确的记录,即当输入 A 时所有 A 记录显示,B 输入所有 B 记录显示等等。但是,是否可以在组合框中找不到记录时显示消息框?
The coding i have so far is :-
我到目前为止的编码是:-
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
回答by varocarbas
Achieving what you want with your code is straightforward, just convert If Index = -1 Then Returninto:
用你的代码实现你想要的很简单,只需转换If Index = -1 Then Return为:
If Index = -1 Then
MessageBox.Show("Not Found.")
Return
End If
In any case, note that there is an in-built functionality in ComboBoxperforming the same action which your code does right now: AutoCompleteModedifferent than Noneand AutoCompleteSourceset to ListItems. Logically, you can evolve your code to perform much more complex actions (what I guess that is the case), but I preferred to highlight this issue just in case.
在任何情况下,请注意,在ComboBox执行您的代码现在所做的相同操作时,有一个内置功能:AutoCompleteMode不同于None并AutoCompleteSource设置为ListItems。从逻辑上讲,您可以改进代码以执行更复杂的操作(我猜是这种情况),但我更愿意强调这个问题以防万一。

