關於PB中列表框函數FindItem函數的認識。

在列表框中使用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
當然如果列表框不可編輯且列表中元素左邊不包含其他元素,這種問題是不會出現的。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章