简体   繁体   English

在 VB6 中输入或编辑文本后具有过滤值的下拉列表

[英]Dropdown With filtered value after text entered or edited in VB6

I need dropdown list with text field where i can type and dropdown will filter on basis of text.我需要带有文本字段的下拉列表,我可以在其中键入内容,下拉列表将根据文本进行过滤。
Like I have data of all countries name and I type "AME" in my textbox so only country containing word "AME" it will show list of countries containing character "AME" only and I can select from list (Like America).就像我有所有国家名称的数据,我在文本框中输入“AME”,所以只有包含单词“AME”的国家才会显示包含字符“AME”的国家列表,我可以从列表中输入 select(比如美国)。

I tried using combo box我尝试使用组合框

Private Sub Combo1_GotFocus()
    Combo1.AddItem "America"
    Combo1.AddItem "Europe"
    Combo1.AddItem "China"
    Combo1.AddItem "INDIA"
    Combo1.AddItem "London"
End Sub

There are 2 issue I am facing我面临两个问题

  1. When combo box got focus the dropdown is not opening i have to click on arrow当组合框获得焦点时,下拉菜单未打开,我必须单击箭头
  2. When I type "LO" I need dropdown to filter list with containing data with text "LO" only当我键入“LO”时,我需要下拉列表来过滤包含仅包含文本“LO”的数据的列表
    Like I type "ch" it will only show china and can select china only就像我输入“ch”一样,它只会显示中国并且只能显示 select 中国

Generally, I like using third-party controls for features that are not included with standard controls.通常,我喜欢将第三方控件用于标准控件中未包含的功能。 However, a combination of API calls and the KeyPress event may get you close enough.但是,API 调用和 KeyPress 事件的组合可能会让您足够接近。

For easier coding this solution depends upon a Recordset containing country names.为了简化编码,此解决方案依赖于包含国家名称的 Recordset。

Option Explicit

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 CB_SHOWDROPDOWN = &H14F

Private m_Countries As ADODB.Recordset

Private Sub Form_Load()
   Set m_Countries = New ADODB.Recordset
   m_Countries.Fields.Append "Country", adVarChar, 50
   m_Countries.Open
   m_Countries.AddNew "Country", "America"
   m_Countries.AddNew "Country", "Europe"
   m_Countries.AddNew "Country", "China"
   m_Countries.AddNew "Country", "Chicago"
   m_Countries.AddNew "Country", "INDIA"
   m_Countries.AddNew "Country", "London"
   m_Countries.MoveFirst
   
   Combo1.Text = ""
   
   FillCombo
End Sub

Private Sub Combo1_GotFocus()
   SendMessage Combo1.hwnd, CB_SHOWDROPDOWN, True, ByVal 0
End Sub

Private Sub Combo1_LostFocus()
   SendMessage Combo1.hwnd, CB_SHOWDROPDOWN, False, ByVal 0
End Sub

Private Sub Combo1_KeyPress(KeyAscii As Integer)
   Dim Filter As String
   
   If KeyAscii > 31 Then
      Filter = Combo1.Text & Chr(KeyAscii)             'typed char
   ElseIf KeyAscii = 8 And Combo1.Text <> "" Then
      Filter = Left(Combo1.Text, Len(Combo1.Text) - 1) 'backspace
   End If

   If Filter <> "" Then
      m_Countries.Filter = "Country LIKE '" & Filter & "%'"
   Else
      m_Countries.Filter = ""
   End If

   FillCombo
End Sub

Private Sub FillCombo()
   Dim i As Integer
   
   'to avoid interferring with the textbox of the combo don't use the Clear method
   For i = Combo1.ListCount - 1 To 0 Step -1
      Combo1.RemoveItem i
   Next

   Do While Not (m_Countries.BOF Or m_Countries.EOF)
      Combo1.AddItem m_Countries.Fields("Country").Value
      m_Countries.MoveNext
   Loop
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM