Results 1 to 2 of 2

Thread: Some of the most concise, best code I've ever written

  1. #1

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002

    Some of the most concise, best code I've ever written

    It's very simple and nothing ground-breaking or anything like that. It's basically search behavior.

    So the way it works is that there are four locations - country, state, city and postal code.

    I can't use an Array of ComboBoxes because I use WithEvents in the cLocationManager class. What I did in the class was create an Array of ComboBoxes and then set each one equal to one of the comboboxes passed to it.


    ' Declarations
    dim m_LocationComboBox(MAX_LOCATIONS) as VB.ComboBox
    Public Property Set LocationComboBox(ByRef ComboBoxControl as VB.ComboBox, ByRef Index as LOCATION)
    Set m_LocationComboBox(Index) = ComboBoxControl
    End Property
    This makes it easy to loop through the ComboBoxes to do whatever while still being able to use WithEvents.

    If you type something into the Search box and then click the binoculars next to the location you're searching it will try to find it. If it finds it, it does what you'd expect if you click search again - it goes and looks for the next one.

    If you type something into the textbox for a location (any of the four above) then that will take precedence over what is in the Search box.

    If it doesn't find it then it starts over at the beginning. Again, standard behavior.

    The part where some people might want different behavior is that if you type in something different, the application knows it and starts over from the beginning instead of searching from wherever it is now.

    That's not the behavior I'd want in a word processor but I do like it here.

    And it saves all that per location. So it only resets the box you're searching but not the others.

    Private Sub cmdSearch_Click(Index As Integer)
    SearchForLocation Index
    End Sub
    Private Sub SearchForLocation(ByRef Index As Integer)
    Static nLastIndex(MAX_LOCATIONS) As Long          ' ListIndex (per LocationComboBox) where SearchToken was last found or -1 (FAILED) if not found.
    Static sLastSearchToken(MAX_LOCATIONS) As String  ' Last string searched for per LocationComboBox.
    Dim sSearchToken As String                        ' The Text being searched for.
    ' Searches a LocationComboBox for Text entered by User.
    ' When FAILED is returned the search starts from the Top of the ComboBox unless the ComboBox is unpopulated.
    ' Check that there is something to search for and the LocationComboBox being searched is populated.
    ' ValidSearch chooses the Token to search if Text is entered in either the Search or Location Textbox.
    ' Location Textbox has Priority.
    If ValidSearch(Index, sSearchToken) = FAILED Then GoTo CleanUp
    ' Search Term was changed so start over at top of List.
    If sLastSearchToken(Index) <> sSearchToken Then nLastIndex(Index) = FAILED
    ' Save the SearchToken to compare in subsequent searches to determine if it has changed and to start over from the top of the LocationComboBox.
    sLastSearchToken(Index) = sSearchToken
    ' Begin searching LocationComboBox from the next element after last found.
    nLastIndex(Index) = FindPartialStringInList(LocationComboBox(Index), nLastIndex(Index) + 1, sSearchToken)
    ' Don't change the ListIndex of the LocationComboBox if the SearchToken wasn't found.
    If nLastIndex(Index) = FAILED Then GoTo CleanUp
    LocationComboBox(Index).ListIndex = nLastIndex(Index) ' Display the found item in the LocationComboBox.
    End Sub
    Private Function ValidSearch(ByRef Index As Integer, ByRef SearchToken As String) As Long
    Dim sSearchToken1 As String ' Search Textbox.
    Dim sSearchToken2 As String ' Location being searched Textbox.
    ' Returns 0 if Search is valid.  E.g. there is text to search for and the ComboBox being searched is populated with at least one entry.
    ' Returns FAILED (-1) if the Search Textbox is empty AND the Location Textbox is empty OR if the ComboBox has one or fewer entries.
    ' Provides Appropriate Message to User if Search is not Valid.
    ValidSearch = FAILED ' FAILED = -1. There are fewer cases where the Search is Valid than not so assume Failure.
    sSearchToken1 = Trim$(txtLocation(Index).Text) ' Textbox for Location has priority.
    sSearchToken2 = Trim$(txtSearch.Text)          ' Proper Search Textbox is second.
    If (Len(sSearchToken1) = 0) And (Len(sSearchToken2) = 0) Then ' No text in either Textbox.
      MsgBoxA "Please enter text to search for a " & LocationName(Index) & CHAR_DOT, vbInformation, App.Title
      Exit Function
    End If
    If LocationComboBox(Index).ListCount = 0 Then ' ComboBox has no entries.
      MsgBoxA "There is nothing to search in the " & LocationName(Index) & " Dropdown.", vbInformation, App.Title
      Exit Function
    End If
    SearchToken = IIf(Len(sSearchToken1) > 0, sSearchToken1, sSearchToken2)
    ValidSearch = 0
    End Function
    Last edited by cafeenman; Apr 3rd, 2024 at 09:00 PM.

  2. #2

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002

    Re: Some of the most concise, best code I've ever written

    Also... all the GoTo CleanUp should be changed to Exit Function. I thought I got all of them but missed a couple.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width