Announcement

Collapse
No announcement yet.

set cursor to left side of text box

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

  • set cursor to left side of text box

    I have a use who selects a text box with the mouse. Is there a way to set the cursor to the extreme left side of the text box, no matter where the text box was clicked on? If the user clicks the text box to the right of the text in that box, new text is entered from that point. User wants to start entering text on the left side of box, no matter where the cursor was first placed.

    Thanks for any info.

    John Tate

  • #2
    You mean, like this ?

    Code:
    #COMPILE EXE '#Win 8.04#
    #DIM ALL
    #INCLUDE "Win32Api.inc" '#2005-01-27#
     
    %Textbox1 = 101
    %Textbox2 = 102
    '______________________________________________________________________________
     
    CALLBACK FUNCTION PbMainProc() AS LONG
     LOCAL hTextbox AS DWORD
     LOCAL SelStart AS LONG
     LOCAL SelStop  AS LONG
     
     SELECT CASE CBMSG
     
       CASE %WM_COMMAND
         SELECT CASE CBCTL
     
           CASE %Textbox1
             SELECT CASE CBCTLMSG
               CASE %EN_SETFOCUS 'Textbox is now receiving focus
                 hTextbox = GetDlgItem(CBHNDL, CBCTL) 'Get textbox handle
                 SelStart = 0 'Selection start position
                 SelStop = 0 'Selection stop position
                 PostMessage hTextbox, %EM_SETSEL, SelStart, SelStop 'Set selection and set cursor position
                 'or
                 'CONTROL POST CBHNDL, CBCTL, %EM_SETSEL, SelStart, SelStop
             END SELECT
     
           CASE %Textbox2
             SELECT CASE CBCTLMSG
               CASE %EN_SETFOCUS 'Textbox is now receiving focus
                 hTextbox = GetDlgItem(CBHNDL, CBCTL) 'Get textbox handle
                 SelStart = 0 'Selection start position
                 SelStop = 0 'Selection stop position
                 PostMessage hTextbox, %EM_SETSEL, SelStart, SelStop 'Set selection and set cursor position
                 'or
                 'CONTROL POST CBHNDL, CBCTL, %EM_SETSEL, SelStart, SelStop
             END SELECT
     
         END SELECT
     
     END SELECT
     
    END FUNCTION
    '______________________________________________________________________________
     
    FUNCTION PBMAIN() AS LONG
     LOCAL hDlg AS DWORD
     
     DIALOG NEW %HWND_DESKTOP, "Set cursor", , , 100, 45, %WS_CAPTION OR %WS_SYSMENU, 0 TO hDlg
     SetClassLong hDlg, %GCL_HICON, LoadIcon(BYVAL %NULL, BYVAL %IDI_INFORMATION) 'Set a nice icon
     CONTROL ADD TEXTBOX, hDlg, %Textbox1, "0123456789", 5, 5, 90, 13
     CONTROL ADD TEXTBOX, hDlg, %Textbox2, "0123456789", 5, 25, 90, 13
     DIALOG SHOW MODAL hDlg, CALL PbMainProc
     
    END FUNCTION
    '______________________________________________________________________________
    Last edited by Pierre Bellisle; 31 Oct 2007, 10:09 PM.

    Comment


    • #3
      Thanks, Pierre. That is just what I needed. I had tried some versions of your code, but could not get it to work.

      John Tate

      Comment


      • #4
        Hi John,

        If you feel like it, you could show a little example of a version
        that do not work and hopefully one of us can find a solution.

        Nota: I changed code above to make it a little more clear.

        Comment


        • #5
          Here is a piece of code I was working with. It did not work.
          Code:
          start& = 0:endoit = -1
          
          CASE %IDC_TEXTBOX11
                          IF CBCTL = %IDC_TEXTBOX11 AND CBCTLMSG = %EN_SETFOCUS THEN
                                 CONTROL SET FOCUS CBHNDL,%IDC_TEXTBOX11
                                  CONTROL SEND CBHNDL,%IDC_TEXTBOX11,%EM_SETSEL,START&,ENDOFIT&
                          END IF
          Here is what I am using thanks to Pierre's code.

          Code:
                ' CASE %idc_Textbox11 TO %idc_Textbox19 'set cursor to left side of box and select
                 '    SELECT CASE CBCTLMSG
                  '      CASE %EN_SETFOCUS
                   '          PostMessage GetDlgItem(CBHNDL, CBCTL), %EM_SETSEL, 0, -1
                              'END SELECT
          John Tate

          Comment


          • #6
            I see,

            Note that "CONTROL POST" could also be used instead of "PostMessage"

            The key is to post the message.
            Meaning that windows will finish the job on the textbox control,
            cursor stuff included, before processing our message.

            When "CONTROL SEND" or "SendMessage" is used then
            our cursor stuff is done right away but will be undone
            by Windows when finishing the textbox stuff.

            Comment

            Working...
            X
            😀
            🥰
            🤢
            😎
            😡
            👍
            👎