Announcement

Collapse
No announcement yet.

Set Cursor on listbox item

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

  • Set Cursor on listbox item

    I want to position the cursor on a selected item in a multiple-selection listbox. The following code does not work. What am I doing wrong?

    Code:
    SUB CursorToLBItem(BYVAL hPrnt AS LONG,_
                       BYVAL idlist AS LONG)
        LOCAL hList,cnt AS LONG
        LOCAL rc AS rect
        LOCAL pt AS POINTAPI
    
        hList = GetDlgItem(hPrnt,idlist)
        cnt = Sendmessage(hList,%LB_GETCOUNT,0,0)
        IF cnt THEN
           DIM selitems(cnt - 1) AS LONG
           CALL Sendmessage(hList,%LB_GETSELITEMS,cnt,VARPTR(selitems(0)))
           CALL SendMessage(hList, %LB_GETITEMRECT,selitems(0), VARPTR(rc))
           MapWindowPoints hList, hPrnt, rc, 2
           pt.x = (rc.nRight - rc.nLeft) / 2
           pt.y = rc.nTop + (rc.nBottom - rc.nTop) / 2
           SetCursorPos pt.x,pt.y
        END IF
        END SUB
    www.praecom.de

  • #2
    The documentation to the rescue once again....
    LB_SETSEL Message
    --------------------------------------------------------------------------------
    An application sends an LB_SETSEL message to select a string in a multiple-selection list box.
    You don't want to move the cursor manually, you just want to set the selection and let Windows handle the cursor.
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      Originally posted by Michael Mattias View Post
      The documentation to the rescue once again....


      You don't want to move the cursor manually, you just want to set the selection and let Windows handle the cursor.
      Sorry, "my" windows XP needs obviously an additional magic word. LB_SETSEL hightlights the item but does not move the cursor on it.
      www.praecom.de

      Comment


      • #4
        Gerhard, I think %LB_SetCaretIndex is what you want. I set up a little demo below to illustrate. The dotted rectangle is not drawn until you press a keyboard arrow key, however. Maybe somone has a fix for this.

        Code:
        #COMPILE EXE
        #DIM ALL
        #INCLUDE "win32api.inc"
        
        %IDC_Listbox1 = 101
        %IDC_SetCursor = 102
        GLOBAL hDialog1 AS LONG
        DECLARE CALLBACK FUNCTION Dialog1Proc()
        DECLARE SUB Dialog1()
        
        FUNCTION PBMAIN()
           'Program statements
           Dialog1
        END FUNCTION
        
        SUB Dialog1()
           DIALOG NEW PIXELS, 0, "", , , 300, 220, %WS_SYSMENU OR %WS_MINIMIZEBOX TO hDialog1
           CONTROL ADD LISTBOX, hDialog1, %IDC_Listbox1, , 20, 10, 250, 150, %WS_VSCROLL _
                    OR %LBS_MULTIPLESEL, %WS_EX_CLIENTEDGE 
           CONTROL ADD BUTTON, hDialog1, %IDC_SetCursor, "Set Cursor", 50, 180, 90, 30         
           DIALOG SHOW MODAL hDialog1 CALL Dialog1Proc
        END SUB
        
        CALLBACK FUNCTION Dialog1Proc()
           LOCAL i, n AS LONG, s AS STRING
           SELECT CASE CBMSG
              CASE %WM_INITDIALOG
                 FOR i = 1 TO 50
                    s = "This is line number " + STR$(i)
                    LISTBOX ADD CBHNDL, %IDC_Listbox1, s
                 NEXT i
              CASE %WM_PAINT
              CASE %WM_COMMAND
                 SELECT CASE CBCTL
                 CASE %IDC_SetCursor 
                    n = 25
                    CONTROL SEND CBHNDL, %IDC_Listbox1, %LB_SetCaretIndex, n - 1, 1
                 END SELECT
              CASE %WM_SYSCOMMAND
              CASE %WM_DESTROY
           END SELECT
        END FUNCTION

        Comment


        • #5
          I tried %LB_SetCaretindex before without success. In the meantime I got a "private message" by this forum, and the problem is solved.
          I changed the line

          MapWindowPoints hList, hPrnt, rc, 2

          by

          MapWindowPoints hList, %HWND_DESKTOP, rc, 2,

          and now it works. Thanks for your help!
          www.praecom.de

          Comment


          • #6
            Gerhard,
            Does it involve the mouse pointer moving to the item?

            I have code that will programmatically move to a control, but do not think I can make it move to a list item, nor a certain menu item (just yet), so I would be interested in what the solution (or semi-solution) may be.

            Engineer's Motto: If it aint broke take it apart and fix it

            "If at 1st you don't succeed... call it version 1.0"

            "Half of Programming is coding"....."The other 90% is DEBUGGING"

            "Document my code????" .... "WHYYY??? do you think they call it CODE? "

            Comment


            • #7
              >I have code that will programmatically move to a control

              SetFocus() is pretty handy, huh?

              FWIW, SetCaretIndex() + SetFocus() should 'work'... except that SetCursor does not change the keyboard focus, meaning the cursor is in one place but the focus in another. That's gotta be a lot of fun.
              Michael Mattias
              Tal Systems (retired)
              Port Washington WI USA
              [email protected]
              http://www.talsystems.com

              Comment

              Working...
              X