在列表框中使用FindItem函数查找某一个值是否在列表中存在,在实际应用中发现FindItem并不是我想要的结果,查看一下帮助发现:
Description
Finds the next item in a ListBox that begins with the specified search text.
Controls
ListBox, DropDownListBox, PictureListBox, and DropDownPictureListBox controls
Syntax
listboxname.FindItem ( text, index )
Argument Description
listboxname The name of the ListBox control in which you want to find an item.
text A string whose value is the starting text of the item you want to find.
index The number of the item just before the first item to be searched. To search the whole list, specify 0.
Return value
Integer. Returns the index of the first matching item. To match, the item must start with the specified text; however, the text in the item can be longer than the specified text. If no match is found or if an error occurs, FindItem returns -1. If any argument's value is NULL, FindItem returns NULL.
从上面可以看出,查找并不是查找Text值完全匹配或相等的,而是从列表中查找开始部分,只要前面匹配就会返回其节点的值,故使用时 需要小心。
如
ddlb_1.AddItem('abcdefg')
ddlb_1.AddItem('lkjhgh')
如果想查找列表中是否包含‘lkj’这个节点ddlb_1.FindItem('lkj',0) 此时会返回2,但该节点不是需要选择,所以需要小心。
当然我也可以自己来完成这个功能,如:
String ls_find
Long ll_index,ll_find
For ll_index = 1 To ddlb_1.TotalItems()
If ddlb_1.Text(ll_index) = ls_find Then
ll_find = ll_index
End If
Next
Return ll_find
当然如果列表框不可编辑且列表中元素左边不包含其他元素,这种问题是不会出现的。
Description
Finds the next item in a ListBox that begins with the specified search text.
Controls
ListBox, DropDownListBox, PictureListBox, and DropDownPictureListBox controls
Syntax
listboxname.FindItem ( text, index )
Argument Description
listboxname The name of the ListBox control in which you want to find an item.
text A string whose value is the starting text of the item you want to find.
index The number of the item just before the first item to be searched. To search the whole list, specify 0.
Return value
Integer. Returns the index of the first matching item. To match, the item must start with the specified text; however, the text in the item can be longer than the specified text. If no match is found or if an error occurs, FindItem returns -1. If any argument's value is NULL, FindItem returns NULL.
从上面可以看出,查找并不是查找Text值完全匹配或相等的,而是从列表中查找开始部分,只要前面匹配就会返回其节点的值,故使用时 需要小心。
如
ddlb_1.AddItem('abcdefg')
ddlb_1.AddItem('lkjhgh')
如果想查找列表中是否包含‘lkj’这个节点ddlb_1.FindItem('lkj',0) 此时会返回2,但该节点不是需要选择,所以需要小心。
当然我也可以自己来完成这个功能,如:
String ls_find
Long ll_index,ll_find
For ll_index = 1 To ddlb_1.TotalItems()
If ddlb_1.Text(ll_index) = ls_find Then
ll_find = ll_index
End If
Next
Return ll_find
当然如果列表框不可编辑且列表中元素左边不包含其他元素,这种问题是不会出现的。