查看項目是否在列錶框控件中的最有效方法 (Most efficient way to see if an item is or is not in a listbox control)


問題描述

查看項目是否在列錶框控件中的最有效方法 (Most efficient way to see if an item is or is not in a listbox control)

此請求基於 MS Access VBA。我想知道最有效的方法是什麼,看看某個項目是否存在於列錶框控件中。


參考解法

方法 1:

Here is a sample function that might be adapted to suit.

Function CheckForItem(strItem, ListB As ListBox) As Boolean
Dim rs As DAO.Recordset
Dim db As Database
Dim tdf As TableDef

    Set db = CurrentDb

    CheckForItem = False

    Select Case ListB.RowSourceType
        Case "Value List"
            CheckForItem = InStr(ListB.RowSource, strItem) > 0

        Case "Table/Query"
            Set rs = db.OpenRecordset(ListB.RowSource)

            For i = 0 To rs.Fields.Count ‑ 1
                strList = strList & " & "","" & " & rs.Fields(i).Name
            Next

            rs.FindFirst "Instr(" & Mid(strList, 10) & ",'" & strItem & "')>0"

            If Not rs.EOF Then CheckForItem = True

        Case "Field List"

            Set tdf = db.TableDefs(ListB.RowSource)

            For Each itm In tdf.Fields
                If itm.Name = strItem Then CheckForItem = True
            Next

    End Select

End Function

方法 2:

Unfortunately there is no more efficient way than a linear search, unless you know that your listbox is sorted or indexed in some particular fashion.

For i = 1 To TheComboBoxControl.ListCount
  if TheComboBoxControl.ItemData(i) = "Item to search for" Then do_something()
Next i

方法 3:

If you don't mind resorting to the Windows API you can search for a string like this:

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long   
Private Const LB_FINDSTRINGEXACT = &H1A2

Dim index as Integer
Dim searchString as String
searchString = "Target" & Chr(0)

index = SendMessage(ListBox1.hWnd, LB_FINDSTRINGEXACT , ‑1, searchString)

Which should return the index of the row that contains the target string.

(by Curtis InderwiescheFionnualaSparrRikalous)

參考文件

  1. Most efficient way to see if an item is or is not in a listbox control (CC BY‑SA 2.5/3.0/4.0)

#controls #listbox #vba #ms-access






相關問題

如何在不阻塞的情況下用大量信息填充列表視圖? (How to populate listview with a lot of information without blocking?)

查看項目是否在列錶框控件中的最有效方法 (Most efficient way to see if an item is or is not in a listbox control)

tcng - 匹配“if”語句中的端口範圍 (tcng - match port range in "if" statement)

將圖像加載到面板 (Load image to panel)

Cycle2 Plugin Ẩn / Hiện các nút jQuery (Cycle2 Plugin Hide/Show buttons jQuery)

如何使用 Windows 資源管理器顯示項目和詳細信息展開/折疊 (How to Display Items and Details with Windows Explorer Expand/Collapse)

ASP.NET 2.0 JQuery AJAX 登錄 (ASP.NET 2.0 JQuery AJAX Login)

單步執行控件看不到第二個下拉菜單 (Step through controls fail to see second dropdown)

用戶控件、服務器控件和自定義控件之間有什麼區別? (What are the differences between User Controls, Server Controls & Custom Controls?)

GroupBox 控件中的圓角 (Rounded corners in GroupBox control)

在 .NET 中編寫大型多步驟表單的最有效方法是什麼? (Most efficient way to code a large multi-step form in .NET?)

如何從 ASP .NET MVC 中的動態生成控件中獲取數據? (How can I get data from dynamic generated controls in ASP .NET MVC?)







留言討論