包含带外卡的组合框功能 [英] Contains function of combo box with wild cards
问题描述
我需要检查我的组合框是否包含部分值。
例如,它包含ABC测试
当用户键入ABC时,它应该显示文本已经存在的消息
我可以使用像
这样的通配符
Hi,
I need to check if my combo box contains a part of value or not.
For example, it contains ABC Test
When user types ABC, it should give a message the text is already there
Can I use wildcards like
If ComboBox1.Items.Contains("*" & "ABC" & "*") Then
谢谢
我有什么试过:
用Google搜索并做了很多实验
Thanks
What I have tried:
Googled and have done lots of experiments
推荐答案
没有内置功能,所以你必须自己做。
这应该工作(这是一种扩展方法):
There is no inbuilt functionality for this so you have to make your own.
This should work (it's an extension method):
Module ComboBoxExt
<Runtime.CompilerServices.Extension>
Public Function ContainsItemWithSubstring(cb As ComboBox, substring As String) As Boolean
If cb.Items.Count = 0 Then
Return False
End If
If TypeOf cb.Items(0) Is DataRowView Then
If cb.DisplayMember IsNot Nothing AndAlso TypeOf DirectCast(cb.Items(0), DataRowView)(cb.DisplayMember) Is [String] Then
For Each item As Object In cb.Items
If DirectCast(DirectCast(item, DataRowView)(cb.DisplayMember), [String]).Contains(substring) Then
Return True
End If
Next
End If
Else
For Each item As Object In cb.Items
If TypeOf item Is [String] AndAlso DirectCast(item, [String]).Contains(substring) Then
Return True
End If
Next
End If
Return False
End Function
End Module
它适用于任何对象的正常Items集合(显然只对字符串有意义)和一个绑定到DataTable的ComboBox或DataView提供了.DisplayMember属性设置。
您可以这样使用它:
It works with a normal Items-collection of whatever-objects (obviously only makes sense with strings) and with a ComboBox that's bound to a DataTable or DataView provided that the .DisplayMember-property has been set.
You can then use it like this:
If MyComboBox.ContainsItemWithSubstring("ABC") Then
这篇关于包含带外卡的组合框功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!