dcsimg
Results 1 to 15 of 15

Thread: [RESOLVED] Adding to combobox in reverse order

  1. #1

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,222

    Resolved [RESOLVED] Adding to combobox in reverse order

    I am using a combobox to keep track of the five most recent searches one makes in my program.

    After user types in the new search into a combobox and clicks a cmd button, I add that search text to the combobox. I maintain a blank (" ") at listindex(0).
    Purpose, so that user can quickly use most recent 5 searches without typing.

    So, currently if 5 have been done, I do not add the most recent to the box...(if less than 5 then I add the new search with AddItem---this results in the combobox having 5 searches (the most recent is the last one)--for example:

    " "
    "SAM"
    "DILE"
    "ELROY"
    "LAVOLPE"

    Without the quotes, of course. In that example Sam was searched for first, and Lavolpe last (not concerned with case). I would like the box to look like this:

    " " (Always first)
    "LAVOLPE" (LAST searched)
    "ELROY"
    "DILE"
    "SAM" (FIRST searched)

    And of course, if a fifth (because am not counting the " ") is searched, like "SALSA" (remember him???), it would look like this ("SAM" would disappear)

    " "
    "SALSA"
    "LAVOLPE"
    "ELROY"
    "DILE"

    This saves the most recent searches and orders them from listindex(1) to listindex(4).

    Haven't quite figured out how to do this...

    Sam

    (headed out for 3 hours)

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,991

    Re: Adding to combobox in reverse order

    Code:
    AddItem TheItem,0
    adding the ,0 will cause the last added items to always go to the top.

    If you want the "" at the top then I would assume you could use 1 as the index parameter to get the desired result

    Of course the "" must be added first, if the combo is empty using an index of one will throw an error

    Quick test to confirm
    Code:
    Option Explicit
    
    Private Sub Form_Load()
    Dim x As Integer
    Combo1.AddItem ""
    For x = 0 To 10
        Combo1.AddItem CStr(x), 1
    Next
    
    End Sub

  3. #3
    Fanatic Member
    Join Date
    Nov 2017
    Posts
    847

    Re: Adding to combobox in reverse order

    Code:
      If Combo1.ListCount < 6 Then
        Combo1.AddItem strSearch, 1
      End If
    Or to do a rolling last five in top-down order:

    Code:
      Combo1.AddItem strSearch, 1
      If Combo1.ListCount > 6 Then
        Combo1.RemoveItem (6)
      End If

  4. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,991

    Re: Adding to combobox in reverse order

    I use a similar method in my server list box where it adds recent transactions at the top of the list and then drops items off the bottom.

    To drop them I just check the listcount and if greater than the desired number use the removeitem method to remove the unwanted item(s) from the end of the list.

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,524

    Re: Adding to combobox in reverse order

    Sam, from the way I'm reading this...

    1. Always use: Combo1.AddItem strSearch, 1 to place the latest search after the blank (already suggested)
    2. Before appending the latest search, see if it already exists. If it does, remove it & if not, remove the last list item if ListCount > 1. You can search with a simple FOR:NEXT loop
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  6. #6
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    1,998

    Re: Adding to combobox in reverse order

    Maybe something to consider:
    How about the most used searchphrases?

    From a user's POV it might be more interesting to keep the most used phrases.
    Don't get me wrong: to keep the last phrases has its uses, no argument. Just something to consider.

    In that case you would need to keep a counter and sort the entries by count with the one with highest count being the top one (BLANK not withstanding) in descending order.
    You could use one of the approaches mentioned here:
    http://www.vbforums.com/showthread.p...ox-by-ItemData


    In action your combobox could look like this (a "marriage" of both ideas above)
    [BLANK]
    --- Last Searches ---
    LAVOLPE
    ELROY
    --- Most Searches ---
    SAM (20)
    DILE (18)
    ZVONI (15)
    ELROY (13) --> Up for discussion to add it here when it's already under "Last"
    LAVOLPE (13) --> Up for discussion to add it here when it's already under "Last"
    One System to rule them all, One IDE to find them,
    One Code to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    For health reasons i try to avoid reading unformatted Code

  7. #7
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    1,998

    Re: Adding to combobox in reverse order

    Quote Originally Posted by LaVolpe View Post
    2. Before appending the latest search, see if it already exists. If it does, remove it & if not, remove the last list item if ListCount > 1. You can search with a simple FOR:NEXT loop
    Or with SendMessage CB_FINDSTRING
    Yes, not the dealbreaker with 5/6 items in a Listbox, but i like to keep it straight
    One System to rule them all, One IDE to find them,
    One Code to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    For health reasons i try to avoid reading unformatted Code

  8. #8

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,222

    Re: Adding to combobox in reverse order

    Yup---simple....just didn't take time to think it through...sorry for bothering y'all.

    @Lavolpe...yuh, I already check to see if the search item is in the box, if not, add it (currently, only if number is less than 5, but would not do it that way , but, as suggested, simply remove the highest listindex (thx for reminding, DataMiser) after the '5' limit has been reached (BTW, 5 is just what I thought would be sufficient, but could keep more obviously).

    I like Z's idea as well, both Recent and Most...will probably implement that.

  9. #9

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,222

    Re: [RESOLVED] Adding to combobox in reverse order

    @Z (and everyone else_...simple cbox done (most recent) ordered as desired.) As you said 'up for discussion...', probably I will not add a hybrid cbox, but rather and when a search is repeated, simply put the number of times in parentheses in the 'most recent'...whaddya think?

    So it would look like

    "" (meaning a blank)
    "LAVOLPE(12)"
    "ELROY(10)"
    "ZVONI(23)


    The MOST RECENT being LAVOLPE

  10. #10

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,222

    Re: [RESOLVED] Adding to combobox in reverse order

    As I already using an Access DB in my app, simple enough to create a table to hold that data (search item and number of times searched). And with only, say 5, searches being saved, the table would never (I say never, but doubtful it would ever) get too big. But that brings into the issue of how many SHOULD be saved and counted. A most recent may never hit that 'most searched'....gotta really think how I want to implement something that keeps track of numbers of searches. Interesting concept though.

    One thing I would not want is a real long cbox...

  11. #11
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,991

    Re: [RESOLVED] Adding to combobox in reverse order

    Well it should not be a problem to save even a few thousand in a db then pull something like the top 10 for the combo, or may even want to update the combo as they begin to type showing possible common searches that start with whatever they have typed so far.

  12. #12
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    1,998

    Re: [RESOLVED] Adding to combobox in reverse order

    Quote Originally Posted by SamOscarBrown View Post
    @Z (and everyone else_...simple cbox done (most recent) ordered as desired.) As you said 'up for discussion...', probably I will not add a hybrid cbox, but rather and when a search is repeated, simply put the number of times in parentheses in the 'most recent'...whaddya think?

    So it would look like

    "" (meaning a blank)
    "LAVOLPE(12)"
    "ELROY(10)"
    "ZVONI(23)


    The MOST RECENT being LAVOLPE

    I don't think you'll gain anything from this, because the most searched phrases should act as a kinda shortcut.
    With your approach: Everytime you use a phrase, you run the into the possibility of throwing out your most searched phrase at the bottom, just because the last 5 phrases were single searches, so you'd lose the "advantage" of most searched phrases, if they're not "permanently" entered in your combobox.
    e.g. your list above: Say, your next searches are "SCHMIDT", "DATAMISER" and "BONNIEWEST" (and each phrase only searched once). You'd push "ZVONI(23)" out at the bottom.

    The Number of searches in parantheses' only makes sense if you keep them outside of "recent" searches.
    But that's just my 2 cents ... It's your program...
    One System to rule them all, One IDE to find them,
    One Code to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    For health reasons i try to avoid reading unformatted Code

  13. #13

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,222

    Re: [RESOLVED] Adding to combobox in reverse order

    Good 2 cents...that thought entered my mind and was trying to see how not to make that happen. I guess I'll take your first suggestion and use a hybrid one, keeping 'a few thousand' (which probably would not occur in MY program (currently the ONLY user--but if I share it with friends (no a marketing project), they too could keep 'a few thousand' in the db), so this should not be difficult at all. When (if) I do it, I will create a miniature example program (with DB) and post so anyone who visits might benefit if they want to do something similarly.

    Thanks.

  14. #14

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,222

    Re: [RESOLVED] Adding to combobox in reverse order

    Argh...dummy me! Not even thinking: when I quit the program I 'lose' all that 'information' (Most Recent)...so I need something anyway to keep it. As I already have a DB, I'll save it each time, hence keeping track of the NUMBER OF TIMES each item is searched will be likewise saved.

    Back to work!

  15. #15

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,222

    Re: [RESOLVED] Adding to combobox in reverse order

    Well...got it all in my project. Saves all searches, shows last five (most recent, top to bottom), and also five of the most popular searches.

    Put together a small project using same ideas/similar code...attached.

    (Yes, it does not use "?"'s in queries, and yes, uses an older way of accessing the tables, but it works fine, and should give an idea on how to implement in anyone else's programs.)

    Sammi

    ComboBoxSearches.zip

    Thanks to all, but specific thanks to DataMiser and Zvoni...got me on the right track.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width