Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Listbox search

Status
Not open for further replies.

finitesimian

Programmer
Feb 11, 2006
29
0
0
US
I'm trying to make a form with a listbox control that outputs strings based on information typed into a textbox control------however, the way I WANT to do it is have it incrementally list the words (from an array) as the user is entering the data. For example - If the user enters the letters "com", the listbox displays all the words that start with the letter "c".....then "co"....then "com" as the person is typing (the list obviously reduces itself in size as the person types).

I have it worked out conceptually......but I haven't figured out the right way to say it. Here is an example:

Code:
Declare function findname(identifier$, action, suppvalue)
Global output_array(9) As String



'----------------------------------------
Sub Main()
 


' opens dialog box        
Begin Dialog newdlg 91, 36, 209, 127, "Locate name:", .findname
   OkButton  153, 12, 50, 14
   CancelButton  153, 30, 50, 14
   ListBox  5, 52, 202, 74, output_array, .ListBox1
   TextBox  5, 33, 124, 13, .TextBox1
   OptionGroup .OptionGroup1
      OptionButton  6, 4, 85, 10, "Name", .OptionButton1
      OptionButton  6, 18, 85, 10, "Location", .OptionButton2
End Dialog


Dim mydialog As newdlg     
On Error Resume Next
Dialog mydialog  
          
If Err=102 Then
Stop
End if


'Here is what it needs to do: 

'Dim wordscan As String
'For x = 1 to 9
    'zor = len(mydialog.textbox1)
    'wordscan = left(county_name(x),zor)
    'If wordscan = left(mydialog.textbox1,zor) then
          'n = n + 1
          'redim output_array(n)
          'output_array(n) = county_name(x)
    'End if
'Next




End sub
'----------------------------------------



'This function needs to control the list of words in the listbox.
'I know my "for next" statement needs to go here somewhere
'I just don't know where to put it or how to say it
Function findname(identifier$, action, suppvalue)
Select Case action
Case 1                      
Case 2 
     Select case DlgControlID(identifier$)
          Case 5
          output_array(0) = "Adam" 
          output_array(1) = "Bernard"
          output_array(2) = "Corey"
          output_array(3) = "David"
          output_array(4) = "Eric"
          output_array(5) = "Frank"
          output_array(6) = "George"
          output_array(7) = "Henry"
          output_array(8) = "Igor" 
          output_array(9) = "Jason" 
          Case 6
          output_array(0) = "Arkansas" 
          output_array(1) = "Boston"
          output_array(2) = "California"
          output_array(3) = "Detroit"
          output_array(4) = "Eerie"
          output_array(5) = "Frankfurt"
          output_array(7) = "Grand Rapids"
          output_array(8) = "Holland" 
          output_array(9) = "Illinois"
          End Select
                  
               Case 3
 
               Case 4     
                    
               Case 5                    
End select
End Function

 
Code:
Declare function findname(identifier$, action, suppvalue)
Dim name_array() As String
Dim loc_array() As String
Dim search As String

Sub Main()
  ReDim name_array(9)
  name_array(0) = "Adam"
  name_array(1) = "Bernard"
  name_array(2) = "Corey"
  name_array(3) = "David"
  name_array(4) = "Eric"
  name_array(5) = "Frank"
  name_array(6) = "George"
  name_array(7) = "Henry"
  name_array(8) = "Igor"
  name_array(9) = "Jason"

  ReDim loc_array(9)
  loc_array(0) = "Arkansas"
  loc_array(1) = "Boston"
  loc_array(2) = "California"
  loc_array(3) = "Detroit"
  loc_array(4) = "Eerie"
  loc_array(5) = "Frankfurt"
  loc_array(7) = "Grand Rapids"
  loc_array(8) = "Holland"
  loc_array(9) = "Illinois"

  Begin Dialog newdlg 91, 36, 209, 127, "Locate name:", .findname
    OkButton  153, 12, 50, 14
    CancelButton  153, 30, 50, 14
    ListBox  5, 52, 202, 74, "", .ListBox1
    TextBox  5, 33, 124, 13, .TextBox1
    OptionGroup .OptionGroup1
      OptionButton  6, 4, 85, 10, "Name", .OptionButton1
      OptionButton  6, 18, 85, 10, "Location", .OptionButton2
  End Dialog

  Dim mydialog As newdlg     
  On Error Resume Next
  Dialog mydialog         
  If Err=102 Then
    Stop
  End if
End Sub


Function findname(identifier$, action, suppvalue)
  Dim new_array() As String
  Select Case action
    Case 1                      

    Case 2
      search = DlgText("TextBox1")
      Select case identifier$
        Case "OptionButton1"         
          If search <> "" Then
            cnt = 0
            For i = 0 to 9
              If InStr(1, name_array(i), search, 1) = 1 Then
                ReDim Preserve new_array(cnt)
                new_array(cnt) = name_array(i)
                cnt = cnt + 1
              End If
            Next
            DlgListBoxArray "ListBox1", new_array
          Else
            DlgListBoxArray "ListBox1", name_array
          End If

       Case "OptionButton2"         
          If search <> "" Then
            cnt = 0
            For i = 0 to 9
              If InStr(1, loc_array(i), search, 1) = 1 Then
                ReDim Preserve new_array(cnt)
                new_array(cnt) = loc_array(i)
                cnt = cnt + 1
              End If
            Next
            DlgListBoxArray "ListBox1", new_array
          Else
            DlgListBoxArray "ListBox1", loc_array
          End If
      End Select

    Case 3
    
    Case 4
       
    Case 5
    
  End select
End Function

This is the closest you will get with EB due to the way dialog box events are handled.

[tt]The following table summarizes the possible action% values and their meanings:
action% Meaning
1 Dialog box initialization. This value is passed before the dialog box becomes visible.
2 Command button selected or dialog box control changed (except typing in a text box or combo box).
3 Change in a text box or combo box. This value is passed when the control loses the input focus: the user presses the TAB key or clicks another control.
4 Change of control focus. Id$ is the id of the dialog control gaining focus. Suppvalue& contains the numeric id of the control losing focus. A dialog function cannot display a message box or dialog box in response to an action value 4.
5 An idle state. As soon as the dialog box is initialized (action% = 1), the dialog function will be continuously called with action% = 5 if no other action occurs. If dialog function wants to receive this message continuously while the dialog box is idle, return a non-zero value. If 0 (zero) is returned, action% = 5 will be passed only while the user is moving the mouse. For this action, Id$ is equal to empty string ("") and suppvalue& is equal to the number of times action 5 was passed before.[/tt]

While you might think 5 would work (in testing I found it entirely useless and unreliable), 3 prevents the textbox value never updates until the control loses focus. So, while you might be able to check the value during idle, the value will not have updated until you textbox control loses focus.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top