Getting the Caret Location
-
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
-
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
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 "+".
-
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 "+".
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
-
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
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?
-
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?
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
-
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
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...
-
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?
-
If you are interested I put together a simple demo which is shows this happening. Chris Mancini
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.