Announcement

Collapse
No announcement yet.

Dynamically changing the style of a ListBox

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Dynamically changing the style of a ListBox

    How can I turn off/on the AutoSort feature of a Listbox?
    I've tried any number of Styles but it always seems to stay on.
    What I'd really like to do is change styles dynamically like can be done with fonts:
    Code:
    ' 
    Macro m_Listbox_Courier
     Courier_14 'in fonts.inc
      Control Send hDlg, %LB_01, %WM_SETFONT, hFont, 0
    End Macro
    '
    Macro m_Listbox_Comic
     Comic_16 'in fonts.inc
      Control Send hDlg, %LB_01, %WM_SETFONT, hFont, 0
    End Macro
    '
    sort of like:
    Code:
    '
    Macro m_Listbox_Sort_On
     Stile = %LBS_Standard
      Control Send hDlg, %LB_01, %WM_Set_Style, Stile, 0
    End Macro                          ' ^^^^^^^^^^ <-- Which one to use?
    '
    '
    Macro m_Listbox_Sort_Off
     Stile = %LBS_??? '<-- Which one to use? 
      Control Send hDlg, %LB_01, %WM_Set_Style, Stile, 0
    End Macro                           ' ^^^^^^^^^^^^ <-- Which ones to use?
    '
    ==========================
    "Don't be so humble
    you are not that great."
    Golda Meir (1898-1978)
    to a visiting diplomat
    ==========================
    It's a pretty day. I hope you enjoy it.

    Gösta

    JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
    LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

  • #2
    Don't use style LBS_SORT when you create the listbox. (Create code not shown)

    You 'might' be able to change it with 'SetWindowLong hWnd %GWL_STYLE, new_style_not_including_lbs_sort' but for the life of me I cannot visualize an application where you'd want to change between sort and no-sort styles dynamically.
    Michael Mattias
    Tal Systems Inc. (retired)
    Racine WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      Originally posted by Michael Mattias View Post
      Don't use style LBS_SORT when you create the listbox. (Create code not shown)
      I thought I covered CNS when I said I had tried various Styles. I've tried various Styles but but Sorting is either On or Off. However here's the setup:

      Code:
       Col = 10
       Row = 10
       Tb_Height = Box_Height * 20
       Tb_Len = Wdth - col - 10
       Stile = %LBS_NOSEL 'Sorting off
       Control Add ListBox, hdlg, %LB_01,  , _
             Col, Row, _
              Tb_Len, Tb_Height,_
             Stile, _
             Call CB_ListBox
      You 'might' be able to change it with 'SetWindowLong hWnd %GWL_STYLE, new_style_not_including_lbs_sort'
      Thanks, I'll try that. Wouild rather stay with DDT but if API is necessary, I'll give it a shot.
      but for the life of me I cannot visualize an application where you'd want to change between sort and no-sort styles dynamically.
      I know exactly how you feel. Just the other day I was feeling the same way about 300 controls on a single screen.

      Actually the dialog consists of a single ListBox and I'm hoping to be able to send messages to the user via the listbox, as well as display various lists.
      It's a pretty day. I hope you enjoy it.

      Gösta

      JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
      LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

      Comment


      • #4
        Withdrawn for Terminal Dumbness.

        Am going back to Square One. (note - SetWindowLong did not work, at least for me.)
        Last edited by Gösta H. Lovgren-2; 2 Mar 2008, 01:29 PM. Reason: Correction -Set Window - does NOT work (so far).
        It's a pretty day. I hope you enjoy it.

        Gösta

        JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
        LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

        Comment


        • #5
          I've got a feeling you can't do it. Maybe the approach would be to have an unsorted and a sorted one at the same location and just switch the z-order when you want to change.

          Comment


          • #6
            Exactly. Once it's "Sorted", how would one "Un-Sort" it? A random shuffle?

            Bob Zale

            Comment


            • #7
              I think there's a little confusion here. It's not the same array, just the same Listbox that displays different arrays. What I wanted to do was use an unsorted text array in some circumstances and others to display a (sorted) list, depending on user choices.

              SetWindowLong didn't work. Later I'll look into using Borje's ListView, which may be able to do what I want (not sort the arrays). I used it several years ago but forget just now how it was implemented.

              Otherwise (just occurring to me) I could set the LB Style to (Unsorted) and Sort (or not) before Adding to the LB. {lightbulb popping}

              Thanks, all.

              ===========================
              An investment in knowledge
              pays the best interest.
              Ben Franklin
              ===========================
              It's a pretty day. I hope you enjoy it.

              Gösta

              JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
              LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

              Comment


              • #8
                On those occasions when a 'on the fly' change method is not available I Kill then Add. In other words if diplomacy fails try a twelve bore.

                Comment


                • #9
                  Listboxes do NOT have to be sorted. Period.

                  Absent style LBS_SORT, strings go where you tell them, either with LB_ADDSTRING or LB_INSERTSTRING message.

                  LISTBOX ADD supports this if you want to stay "all-DDT"....
                  If the list box has the %LBS_SORT style, the new string string_expr will be inserted into the list box in alphanumeric order; otherwise it is added to the end of the existing list.
                  Otherwise (just occurring to me) I could set the LB Style to (Unsorted) and Sort (or not) before Adding to the LB. {lightbulb popping}
                  Um, why not just create the listbox without LBS_SORT style, but without content; then sort or not sort the array?

                  Code:
                  FUNCTION Listbox_AddArray(hWnd AS LONG, _
                                                         idctrl AS LONG, _
                                                         s() AS STRING, _ 
                                 OPTIONAL BYVAL bSort AS LONG) AS LONG
                  
                     IF ISTRUE bSort THEN
                        ARRAY SORT S()
                     END IF 
                  
                     FOR Z = LBOUND (S,1) TO UBOUND (s,1) 
                       LISTBOX ADD  hWnd, idctrl, S(Z) 
                     NEXT
                  END FUNCTION
                  
                  
                  
                  CALLBACK FUNCTION DlgProc .......
                      ......
                      .......
                       CASE %WHATEVER_MESSAGE 
                                 REDIM   StringArray (anynumber) 
                                 CALL   FillArray(StringArray())
                                 CALL  Listbox_AddArray (CBHNDL, %ID_LISTBOX, StringArray(), ISTRUE (Sorted_conditional))
                  ...
                  MCM
                  Michael Mattias
                  Tal Systems Inc. (retired)
                  Racine WI USA
                  [email protected]
                  http://www.talsystems.com

                  Comment


                  • #10
                    Originally posted by Michael Mattias View Post
                    Um, why not just create the listbox without LBS_SORT style, but without content; then sort or not sort the array?
                    MCM
                    That's what I meant the lightbulb was. Apparently it wasn't as illuminating as I thought.

                    =============================
                    Let's have some new cliches.
                    Samuel Goldwyn
                    =============================
                    It's a pretty day. I hope you enjoy it.

                    Gösta

                    JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
                    LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

                    Comment


                    • #11
                      hi Gosta

                      i was doing some speed test in loading listboxs.
                      i was reading in 133,000 file names from one directory(no i do not usually have that many files on my whole laptop, but if you going to test, then test) and placing them into an array that i was naming original array.
                      then copying the original array into two other arrays, one called undo array for undoing a process and another called work array for processing.
                      then i sorted the undo array ,then the work array next.
                      then displayed the work array( loaded the array into a listbox).

                      the sorting and loading of the arrays where very fast, but i had to hide the listbox while the array was being loaded. hiding the listbox made the difference in speed while loading a very large array, but it was fast. if i did not hide the listbox, i had time for a coke.

                      every time the user filters the array, i save the work array into the undo array, then processed(sorted and displayed) the work array. it was all very very fast.

                      i am lazy, prior to processing the work array, i use the function redim preserve on the undo array before i load the undo array from the work array.

                      the work array had always been equal or less than the subscripts in the undo array. I did not test any speed where increasing a subscript would have taken place.

                      the conclusion i had came up with is, to handle all sorting of listboxs in separate arrays as opposed to letting something else do this for me in the listbox control and i was always able to control the sort too.

                      i used the built in array sort routines in pb for all sorting.

                      what took the longest time, was to read the file names in for the first time. it took about 20 seconds on my laptop with 5400 spin rates with 1 gig of memory, and 2 to 3 seconds on the reads after the first read.

                      i was very pleased with the outcome, thanks to pb.

                      paul
                      Last edited by Paul Purvis; 3 Mar 2008, 02:35 AM.
                      p purvis

                      Comment


                      • #12
                        Originally posted by paul d purvis View Post
                        hi Gosta

                        i was doing some speed test in loading listboxs.
                        i was reading in 133,000 file names paul
                        Paul, using that many entries into a Listbox, I urge you to look into Borje's (Virtual?) ListView routines. I used them several years ago when manipulating and displaying property tax data from upwards of 40,000 homes in arrays at one time. Virually instantaneous loading/reloading.

                        Do a search for Borje and LV, or if you can't find it let me know and I'll look it up here in my stuff,

                        =========================================
                        "Be nice to people on your way up
                        because you meet them on your way down."
                        Jimmy Durante
                        =========================================
                        It's a pretty day. I hope you enjoy it.

                        Gösta

                        JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
                        LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

                        Comment


                        • #13
                          >if i did not hide the listbox, i had time for a coke.

                          That's true of a lot of controls... the 'default' action is to Add/Change/Delete, then display the changed contents.. and the re-display can be the real time-eater.

                          With most controls you can suppress its urge to display by sending the WM_SETREDRAW message....

                          Code:
                          MACRO Redraw_Off (hCtrl)
                            SendMessage hCtrl, %WM_SETREDRAW, %FALSE, 0&
                          END MACRO
                          MACRO Redraw_On (hCtrl)
                            SendMessage hCtrl, %WM_SETREDRAW, %TRUE, 0&
                          END MACRO
                          
                          .....
                             hCtrl = getDlgITem (hwnd, %ID)   
                             Redraw_Off  (hCtrl)
                             DO While Still_Stuff_to_do
                                  Add/Change/Delete
                             LOOP
                             Redraw_on (hCtrl)
                             ' force redraw now
                             InvalidateRect hCtrl, BYVAL %NULL
                             UpdateWindow  hCtrl, %TRUE
                          This way you won't get the target control disappearing and re-appearing...or flickering whilst loading.... it will just 'do nothing' until you are done changing the contents.

                          BTW, for 133,000 items, I think I'd look REAL REAL hard at making that a 'virtual' control.. assuming I'd put 133,000 rows into a listbox, listview or treeview to begin with.

                          MCM
                          Michael Mattias
                          Tal Systems Inc. (retired)
                          Racine WI USA
                          [email protected]
                          http://www.talsystems.com

                          Comment


                          • #14
                            Originally posted by Michael Mattias View Post
                            ...assuming I'd put 133,000 rows into a listbox, listview or treeview to begin with.
                            If you are writing an application to examine a database, for example, you don't have much say in how many rows might be retrieved by a user's "SELECT *" type query. OK, you can put a LIMIT on it, but that does not always suit the purpose - the user may want to browse the table looking for a single row, rather than writing a query with a finer focus. The ownerdrawn listview is perfect for this.

                            Comment


                            • #15
                              i was reading in 133,000 file names from one directory(no i do not usually have that many files on my whole laptop
                              I know in your case this was just a test thing, but I have a client with a directory which had (past tense) 106,000 files. none of which other than the most recent two (2) had any meaning. (Explorer took a while to show that folder).

                              I 'suggested' this folder was a good place to look when looking for files to delete when they ran out of disk space... which they did one day.

                              It also helped a bit when they emptied the 68,000 objects from the Recycle Bin.

                              (The usual: it was Nobody's job to keep this server clean. "Nobody" seems to make the rounds, since I run into him at multiple client sites.)
                              Michael Mattias
                              Tal Systems Inc. (retired)
                              Racine WI USA
                              [email protected]
                              http://www.talsystems.com

                              Comment


                              • #16
                                >OK, you can put a LIMIT on it

                                Well, sometimes you can .... LIMIT (or equal) is not supported by all DBMSs
                                Michael Mattias
                                Tal Systems Inc. (retired)
                                Racine WI USA
                                [email protected]
                                http://www.talsystems.com

                                Comment

                                Working...
                                X