Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Getting the Caret Location

Getting the Caret Location

Scheduled Pinned Locked Moved Visual Basic
question
8 Posts 2 Posters 31 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Hi, I am trying to retrieve the CaretPosition within a textbox. I have tried the following code within the KeyPress event of the TextBox: Dim ptCaret As POINTAPI Dim lPoint As Long Dim lIndex As Long GetCaretPos ptCaret lPoint = (ptCaret.y * 65536) + (ptCaret.x And &HFFFF&) lIndex = SendMessage(txtPrice.hwnd, EM_CHARFROMPOS, 0, lPoint) If lIndex And &H8000& then nCharIndex = lIndex Or &HFFFF0000 else nCharIndex = lIndex And &HFFFF& End If GetCaretPos returns the correct x,y coordinates, but SendMessage always returns -1. Any ideas???? Chris Mancini

    J 1 Reply Last reply
    0
    • L Lost User

      Hi, I am trying to retrieve the CaretPosition within a textbox. I have tried the following code within the KeyPress event of the TextBox: Dim ptCaret As POINTAPI Dim lPoint As Long Dim lIndex As Long GetCaretPos ptCaret lPoint = (ptCaret.y * 65536) + (ptCaret.x And &HFFFF&) lIndex = SendMessage(txtPrice.hwnd, EM_CHARFROMPOS, 0, lPoint) If lIndex And &H8000& then nCharIndex = lIndex Or &HFFFF0000 else nCharIndex = lIndex And &HFFFF& End If GetCaretPos returns the correct x,y coordinates, but SendMessage always returns -1. Any ideas???? Chris Mancini

      J Offline
      J Offline
      Jason Teagle
      wrote on last edited by
      #2

      This is a rich edit, right? Normal text boxes require a different use of the lParam parameter. Check your lPoint value in hex and make sure it looks OK, in case the attempt to form lPoint went wrong - if it did, try using "&h10000&" instead of "65536", and try using "Or" instead of "+".

      L 1 Reply Last reply
      0
      • J Jason Teagle

        This is a rich edit, right? Normal text boxes require a different use of the lParam parameter. Check your lPoint value in hex and make sure it looks OK, in case the attempt to form lPoint went wrong - if it did, try using "&h10000&" instead of "65536", and try using "Or" instead of "+".

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Thanks for your response. Actually it is a Normal TextBox. I looked over the documentation on MSDN and found that there was a slight problem. I was passing lPoint as ClientCoordinates not ScreenCoordinates, so I fixed that, yet SendMessage is still returning -1. MSDN Says.... "Edit controls: The low-order word contains the horizontal coordinate. The high-order word contains the vertical coordinate." It appears that is what I am doing..... GetCaretPos ptCaret ClientToScreen txtPrice.hwnd, ptCaret lPoint = (ptCaret.y * &H10000) Or (ptCaret.x And &HFFFF&) lIndex = SendMessage(txtPrice.hwnd, EM_CHARFROMPOS, 0, lPoint) If lIndex And &H8000& Then nCharIndex = lIndex Or &HFFFF0000 Else nCharIndex = lIndex And &HFFFF& End If Thanks Chris Mancini

        J 1 Reply Last reply
        0
        • L Lost User

          Thanks for your response. Actually it is a Normal TextBox. I looked over the documentation on MSDN and found that there was a slight problem. I was passing lPoint as ClientCoordinates not ScreenCoordinates, so I fixed that, yet SendMessage is still returning -1. MSDN Says.... "Edit controls: The low-order word contains the horizontal coordinate. The high-order word contains the vertical coordinate." It appears that is what I am doing..... GetCaretPos ptCaret ClientToScreen txtPrice.hwnd, ptCaret lPoint = (ptCaret.y * &H10000) Or (ptCaret.x And &HFFFF&) lIndex = SendMessage(txtPrice.hwnd, EM_CHARFROMPOS, 0, lPoint) If lIndex And &H8000& Then nCharIndex = lIndex Or &HFFFF0000 Else nCharIndex = lIndex And &HFFFF& End If Thanks Chris Mancini

          J Offline
          J Offline
          Jason Teagle
          wrote on last edited by
          #4

          Ignore my rich edit comment... for some reason I thought you were passing lParam incorrectly, but you are not - my mistake. I see no reason why this would not work then. You checked lPoint in hex to make sure it was formed right, did you? Other than that the only other reason I can think of that it would return this (it's actually returning two short integers, both -1: for line number and char number) is if there was no text in the control at the specified point... are you clicking within a portion you KNOW contains text, or are you cliking beyond the text it contains?

          L 2 Replies Last reply
          0
          • J Jason Teagle

            Ignore my rich edit comment... for some reason I thought you were passing lParam incorrectly, but you are not - my mistake. I see no reason why this would not work then. You checked lPoint in hex to make sure it was formed right, did you? Other than that the only other reason I can think of that it would return this (it's actually returning two short integers, both -1: for line number and char number) is if there was no text in the control at the specified point... are you clicking within a portion you KNOW contains text, or are you cliking beyond the text it contains?

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Yes I did check the lPoint value in Hex. It was set up correctly. I am not clicking in the control to trigger the code I am doing the code within KeyPress. I know the textbox has text. And I have tried it when the cursor is at the end of the text as well as in the middle of the text. I am stumped. Chris Mancini

            J 1 Reply Last reply
            0
            • L Lost User

              Yes I did check the lPoint value in Hex. It was set up correctly. I am not clicking in the control to trigger the code I am doing the code within KeyPress. I know the textbox has text. And I have tried it when the cursor is at the end of the text as well as in the middle of the text. I am stumped. Chris Mancini

              J Offline
              J Offline
              Jason Teagle
              wrote on last edited by
              #6

              I'm stumped too. If you had only tried with the caret at the end, I might have understood; MSDN also says: "The index indicates the line delimiter if the specified point is beyond the last visible character in a line." which might explain the -1. But if you've tried it from the middle... I'm not sure what you're trying to achieve, but an alternative might be to keep a copy of the text box's text each time a key is pressed. Then when the next key is pressed, you can get its current contents and compare to the contents saved from last time. That will tell you what character has been deleted or added...

              1 Reply Last reply
              0
              • J Jason Teagle

                Ignore my rich edit comment... for some reason I thought you were passing lParam incorrectly, but you are not - my mistake. I see no reason why this would not work then. You checked lPoint in hex to make sure it was formed right, did you? Other than that the only other reason I can think of that it would return this (it's actually returning two short integers, both -1: for line number and char number) is if there was no text in the control at the specified point... are you clicking within a portion you KNOW contains text, or are you cliking beyond the text it contains?

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                If you are interested I put together a simple demo which is shows this happening. Chris Mancini

                J 1 Reply Last reply
                0
                • L Lost User

                  If you are interested I put together a simple demo which is shows this happening. Chris Mancini

                  J Offline
                  J Offline
                  Jason Teagle
                  wrote on last edited by
                  #8

                  Yes, OK - throw it my way (jteagle@ntlworld.com). Tomorrow I can then take a look and see if I can spot anything (I'm on UK time). No guarantees though.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups