home *** CD-ROM | disk | FTP | other *** search
/ Programming Tool Box / SIMS_2.iso / code / various / listsr / listsrch.bas < prev    next >
BASIC Source File  |  1995-02-12  |  9KB  |  259 lines

  1.   '=======================================================================
  2.   ' This module provides event handling and general routines for the
  3.   ' LISTSRCH.FRM dialog.
  4.   '
  5.   ' Author:   Barth Riley
  6.   '==========================================================================
  7.   Option Explicit
  8.  
  9.   '--Key Code Constants
  10.   Const KEY_BACK = &H8
  11.   Const KEY_DELETE = &H2E
  12.   Const KEY_CLEAR = &HC
  13.  
  14.   '---Focus constants
  15.   Const TEXTBOX_FOCUS = 1       ' currently in text box
  16.   Const LISTBOX_FOCUS = 2       ' currently in list box
  17.  
  18.   '---module level variables
  19.   Dim miCtrlFocus As Integer      ' which control (textbox/listbox) has focus
  20.   Dim miNumKeys As Integer        ' number of keys pressed by user
  21.   Dim mfScrolling As Integer      ' True if textbox triggers listbox scroll.
  22.   Dim mfKeepKey As Integer        ' False if user hit delete/backspace
  23.  
  24. Sub CursorArrow ()
  25.   Screen.MousePointer = 1
  26. End Sub
  27.  
  28. Sub CursorWait ()
  29.   Screen.MousePointer = 11
  30. End Sub
  31.  
  32. Sub LISTSRCH_Activate ()
  33.   MsgBox "Try typing:  " & Chr(34) & "Windows 3.11 for Workgroups" & Chr(34), 64
  34.   frmListSearch.txtSearch.SetFocus
  35. End Sub
  36.  
  37. Sub LISTSRCH_cmdClose_Click ()
  38.   Unload frmListSearch
  39.   End
  40. End Sub
  41.  
  42. Sub LISTSRCH_Load ()
  43.   '---Start of Code
  44.   CursorWait
  45.   frmListSearch.lstSearch.AddItem "Windows"
  46.   frmListSearch.lstSearch.AddItem "Windows 3.1"
  47.   frmListSearch.lstSearch.AddItem "Windows 3.0"
  48.   frmListSearch.lstSearch.AddItem "Windows 3.1 API"
  49.   frmListSearch.lstSearch.AddItem "Winows Program Manager"
  50.   frmListSearch.lstSearch.AddItem "Windows File Manager"
  51.   frmListSearch.lstSearch.AddItem "Windows File Manager API"
  52.   frmListSearch.lstSearch.AddItem "VB"
  53.   frmListSearch.lstSearch.AddItem "VB Programmers Journal"
  54.   frmListSearch.lstSearch.AddItem "VB 1.0"
  55.   frmListSearch.lstSearch.AddItem "VB 2.0"
  56.   frmListSearch.lstSearch.AddItem "VB 3.0"
  57.   frmListSearch.lstSearch.AddItem "VB 3.0 Professional"
  58.   frmListSearch.lstSearch.AddItem "VBX's"
  59.   frmListSearch.lstSearch.AddItem "VB for Applications"
  60.   frmListSearch.lstSearch.AddItem "VB for DOS"
  61.   frmListSearch.lstSearch.AddItem "VBAssist"
  62.   frmListSearch.lstSearch.AddItem "VBA"
  63.   frmListSearch.lstSearch.AddItem "Windows 3.11 for Workgroups"
  64.   frmListSearch.lstSearch.AddItem "Windows 3.11"
  65.  
  66.   miNumKeys = 0
  67.     
  68.   CursorArrow
  69.   '---End of Code
  70. End Sub
  71.  
  72. Sub LISTSRCH_lstSearch_Click (lstSearch As ListBox, txtSearch As TextBox)
  73.   '==========================================================
  74.   ' This routine updates the contents of the txtSearch
  75.   ' text box w. the current list item only if the list box
  76.   ' has the focus and the current list index > 0.
  77.   '============================================================
  78.   '---Variable declarations
  79.   Dim szListText As String
  80.   Dim iListIndex As Integer
  81.   '---Start of Code
  82.   On Error Resume Next
  83.  
  84.   If lstSearch.ListIndex >= 0 And miCtrlFocus = LISTBOX_FOCUS Then
  85.     ' user has clicked on the liat box
  86.     iListIndex = lstSearch.ListIndex
  87.     szListText = lstSearch.List(iListIndex)
  88.     txtSearch.Text = szListText
  89.   End If
  90. End Sub
  91.  
  92. Sub LISTSRCH_lstSearch_KeyDown ()
  93.   '=================================================
  94.   ' KeyDown event handler for the list box. Set
  95.   ' variables to indicate that the user has "cliked"
  96.   ' on the list box
  97.   '====================================================
  98.   miCtrlFocus = LISTBOX_FOCUS
  99.   miNumKeys = 0
  100. End Sub
  101.  
  102. Sub LISTSRCH_lstSearch_MouseDown ()
  103.   '=================================================
  104.   ' MouseDown event handler for the list box. Sets
  105.   ' variables to indciate that the user has clicked
  106.   ' on the list box and now has the focus.  This is
  107.   ' necessary since setting the Selected property of the
  108.   ' list, as well as clicking on the list box, generates
  109.   ' a Click event.
  110.   '====================================================
  111.   miCtrlFocus = LISTBOX_FOCUS
  112.   miNumKeys = 0
  113. End Sub
  114.  
  115. Sub LISTSRCH_txtSearch_Change (lstSearch As ListBox, txtSearch As TextBox)
  116.   '=====================================================
  117.   ' If a new character has been typed into the text box,
  118.   ' this procedure searches the list box for an item
  119.   ' matching the contents of txtSearch.  If found, the
  120.   ' item in the list is selected and the portion of the
  121.   ' text NOT typed by the user is highlighted in the
  122.   ' text box.  Note that mfScrolling is used to prevent
  123.   ' re-entry into this event handler.
  124.   '=====================================================
  125.   '---Variable declaration
  126.   Dim szSrchText As String    ' contents of text box
  127.   Dim iTxtLen As Integer      ' length of search string
  128.   Dim iListIndex As Integer   ' set by SearchListBox
  129.   Dim fReturn As Integer      ' ret. from SearchListBox
  130.   
  131.   '---Start of Code
  132.   On Error Resume Next
  133.  
  134.   If miCtrlFocus = TEXTBOX_FOCUS And mfKeepKey And Not mfScrolling Then
  135.     iTxtLen = Len(txtSearch.Text)
  136.     If iTxtLen Then
  137.       miNumKeys = IIf(miNumKeys < iTxtLen, miNumKeys, iTxtLen)
  138.       szSrchText = txtSearch.Text
  139.       fReturn = SearchListBox(szSrchText, lstSearch, iListIndex)
  140.       
  141.       mfScrolling = True
  142.       If iListIndex = -1 Then
  143.     lstSearch.ListIndex = -1
  144.       Else
  145.     ' perfect match was found
  146.     lstSearch.Selected(iListIndex) = True
  147.     txtSearch = lstSearch.List(lstSearch.ListIndex)
  148.     txtSearch.SelStart = miNumKeys
  149.     txtSearch.SelLength = (Len(txtSearch.Text) - miNumKeys)
  150.       End If
  151.       mfScrolling = False
  152.     End If
  153.   End If
  154. End Sub
  155.  
  156. Sub LISTSRCH_txtSearch_GotFocus ()
  157.   miNumKeys = 0
  158.   frmListSearch.txtSearch.SelStart = 0
  159.   frmListSearch.txtSearch.SelLength = Len(frmListSearch.txtSearch.Text)
  160. End Sub
  161.  
  162. Sub LISTSRCH_txtSearch_KeyDown (ByVal KeyCode As Integer)
  163.   '=====================================================
  164.   ' Determines if a valid (printable) character has been
  165.   ' pressed.  If the character is printable, the
  166.   ' the txtSearch_Change event handler will search
  167.   ' the list box for a matching item.
  168.   '=======================================================
  169.   If KeyCode = KEY_BACK Or KeyCode = KEY_DELETE Or KeyCode = KEY_CLEAR Then
  170.     mfKeepKey = False
  171.     If KeyCode = KEY_BACK Then
  172.       ' unhilight current item; next search
  173.       ' will start at top of list
  174.       frmListSearch.lstSearch.ListIndex = -1
  175.     End If
  176.   Else
  177.     mfKeepKey = True
  178.   End If
  179. End Sub
  180.  
  181. Sub LISTSRCH_txtSearch_KeyPress (KeyAscii As Integer)
  182.   '===============================================================
  183.   ' Keeps track of number of keys pressed
  184.   '===============================================================
  185.   miCtrlFocus = TEXTBOX_FOCUS
  186.   If mfKeepKey Then
  187.     miNumKeys = Len(frmListSearch.txtSearch.Text) + 1
  188.   End If
  189. End Sub
  190.  
  191. Function SearchListBox (ByVal szSearchText As String, lbScroll As ListBox, iListIndex As Integer) As Integer
  192.   '=======================================================
  193.   ' Simple function to create a scrolling list box.
  194.   ' The procedure will select the first item in the list
  195.   ' box in which Left(List box text,size of search string)
  196.   ' matches the search string.
  197.   '==========================================================
  198.   '---Constants (returned from StrComp)
  199.   Const FOUND = 0
  200.   Const LT = -1
  201.   Const GT = 1
  202.   
  203.   '---Variable declarations
  204.   Dim iListStart As Integer     ' starting point in list
  205.   Dim iListCount As Integer     ' no. of items in list box
  206.   Dim iTxtLen As Integer
  207.   Dim szListText As String      ' current list item
  208.   Dim vCompResult               ' result of string comp function
  209.   Dim fFound As Integer         ' match found?
  210.   Dim fDone As Integer          ' Terminates search if true
  211.  
  212.   '---Start of Code
  213.   fFound = False
  214.   iTxtLen = Len(szSearchText)
  215.  
  216.   If iTxtLen > 0 And lbScroll.ListCount > 0 Then
  217.     iListStart = lbScroll.ListIndex
  218.     If iListStart = -1 Then iListStart = 0
  219.     iListIndex = iListStart
  220.     iListCount = lbScroll.ListCount
  221.     szListText = Left(lbScroll.List(iListStart), iTxtLen)
  222.  
  223.     ' check to see if current item matches
  224.     fFound = CInt(StrComp(szSearchText, szListText, 1))
  225.  
  226.     If fFound <> FOUND Then
  227.       fDone = False
  228.  
  229.       If (fFound = LT) Then
  230.     iListIndex = 0
  231.     fFound = False
  232.       Else
  233.     iListIndex = iListIndex + 1
  234.       End If
  235.  
  236.       Do While (iListIndex <= iListCount) And Not fDone
  237.     szListText = Left(lbScroll.List(iListIndex), iTxtLen)
  238.     vCompResult = StrComp(szSearchText, szListText, 1)
  239.     If IsNull(vCompResult) Then
  240.       iListIndex = -1
  241.       Exit Do
  242.     End If
  243.     fFound = (CInt(vCompResult) = FOUND)
  244.     fDone = fFound Or (CInt(vCompResult) = -1)
  245.     If Not fDone Then
  246.       iListIndex = iListIndex + 1
  247.     End If
  248.       Loop
  249.  
  250.       If Not fFound Then
  251.     iListIndex = -1
  252.       End If
  253.     End If
  254.   End If
  255.  
  256.   SearchListBox = fFound
  257. End Function' ScrollListBox
  258.  
  259.