Link to home
Start Free TrialLog in
Avatar of msali
msali

asked on

Rich Text Box Cursor Position

Hi experts,
In my rich text box the user will be adding some specila text of their chocie and then later editing it.  I want to provide the user with some extra help e.g., i want to display some tool tips when they place the typing cursor on a special character.  in order to show the tool tip i need to know 2 things ... (1) The Word on which the cursor is and (2) the co-ordintes of cursor to display the tool tip.

An urgent answer will be much appriciated since i have to add this feature to my app and ship it to the user (hope u understand the urgancy)

thanx in adv
msa.
ASKER CERTIFIED SOLUTION
Avatar of mcrider
mcrider

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
63 / 68
Avatar of msali
msali

ASKER

thnax, and how about which word my cursor is on in the RTB?
Avatar of msali

ASKER

i am lost on ameba's answer 63 / 68
are these some kind of code words?
Yes, "63 / 68" is kind of code... ameba is pointing out your grading record in your profile...

As for getting the word your cursor is on, you can add this function to your program:

    Function GetRTFword(RichTextBox As Object) As String
        Dim lSelStart As Long
        Dim iVal As Long
        Dim jVal As Long
       
        lSelStart = RichTextBox.SelStart
        If lSelStart = 0 Then
            iVal = 1
        Else
            If Mid$(RichTextBox.Text, lSelStart, 1) = " " Then
                iVal = lSelStart + 1
            Else
                iVal = lSelStart - 1
                Do
                    If iVal = 0 Then Exit Do
                    If Mid$(RichTextBox.Text, iVal, 1) = " " Then
                        iVal = iVal + 1
                        Exit Do
                    Else
                        iVal = iVal - 1
                    End If
                Loop
            End If
        End If
        If iVal = 0 Then iVal = 1
        jVal = InStr(iVal, RichTextBox.Text, " ")
        If jVal = 0 Then
            GetRTFword = Mid$(RichTextBox.Text, iVal)
        Else
            GetRTFword = Mid$(RichTextBox.Text, iVal, jVal - iVal)
        End If
    End Function



And then you can call the function like this:

    Dim rtfWord As String
    rtfWord = GetRTFword(RichTextBox1)


Cheers!
Avatar of msali

ASKER

thanx again....but one problem in first answer... i did think about it till last night when i actually started to work on the problem... i.e., getting mouse location was easy but then i realized that the user will have to click the mouse in rtb.... is it possible to get insertion cursor's (Typing Cursor) coordinates instead of mouse coordinates....

......
msa.

ps: 63/68 is good, bad or what? just curious...
Add the following to your program:

     Declare Function GetCaretPos Lib "user32" Alias "GetCaretPos" (lpPoint As POINTAPI) As Long

The GetCaretPos API works just like the GetCursorPos API, only it returns the xy location of the caret (the cursor inside the richtextbox).

To use it, do this:

   Dim CaretLoc as POINTAPI
   GetCaretPos CaretLoc

Then, CaretLoc .x and CaretLoc .y are the screen coordinates of the blinking cursor... relative to the top and left of the richtextbox...


BTW, the 63/68 indicates that you have not graded 5 questions or they were removed.  The experts here don't get paid... We just get points.  We tend to shy away from folks that dont have the decency to grade an answer once they've been "given" the advice.  People that give lousy grades also tend to be ignored.


(hoping for an "A"...)


Cheers!





msali wrote:
>but then i realized that the user will have to click the mouse in rtb

So, for A, you'll probably need some code in MouseMove event, something like:
https://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=10102972 
ameba, I didn't buy Q.10102972 (50 points!), but I don't see how that question relates to this question...

Basically, msali wants to pop up a tooltip if the caret is over one of the "special words" in the RT box.  If you click on the RT box with the mouse, the caret will be placed wherever you click... If you use the arrow keys/page up/page down, the caret will move...

Since the GetCaretPos API returns the location of the caret relative to the position of the RT box, I don't see what you're getting at...
>I didn't buy
Your loss. Maybe you can learn something from others.

>If you click on the RT box with the mouse
Solution: Don't click, don't use these lines
  If pos > -1 Then
     .Selstart = pos
  End If
Ameba, If you have a solution post it... Don't snipe, and dont have other folks buying 50 point questions that may or may not help them.
I posted a link. It shows how to get position in MouseMove from x and y
(over which character is mouse pointer)

When you know position, it is trivial to get the word, i.e. 'over which word is mouse'.

Your code assumes you know position (.SelStart) so it only works when rtb is clicked (.SelStart is changed)
>but then i realized that the user will have to click the mouse in rtb


>Basically, msali wants to pop up a tooltip if the caret is over
No, not caret, but cursor - see question Title.


Pointer to code is valid comment, or perhaps the answer, I think!
Ameba, get your terms right... A caret is a blinking line, block, or bitmap in the client area of a window. The caret typically indicates the place at which text or graphics will be inserted.

See the following microsoft document:

http://msdn.microsoft.com/library/psdk/winui/caret_6qr7.htm 

By the way, if no text is selected in the RT box, the .SelStart property IS the character offset of where is caret is in the RT box... If text is selected, then the caret is still at the same character offset as the .SelStart property, because that is where text will be inserted.
Caret Functions

CreateCaret      Creates a new shape for the system caret
DestroyCaret      Destroys the current caret shape
GetCaretBlinkTime      Retrieves the caret blink rate
GetCaretPos      Retrieves the current caret position
HideCaret      Removes the caret from the screen
SetCaretBlinkTime      Sets the caret blink rate
SetCaretPos      Sets the caret position
ShowCaret      Shows (unhides) the caret on the screen


Cursor Functions

ClipCursor      Confines the cursor to a specified rectangle
CreateCursor      Creates a cursor with specified dimensions
DestroyCursor      Destroys a cursor created by CreateCursor or LoadCursor
GetCursorPos      Retrieves the current cursor position in screen coordinates
LoadCursor      Loads a cursor resource
SetCursor      Changes the mouse cursor
SetCursorPos      Sets the mouse-cursor position in screen coordinates
ShowCursor      Shows or hides the mouse cursor

This part is about cursor:
(1) The Word on which the cursor is

Tooltip can be at cursor position, but also at caret position (e.g. right under the word).
(2) the co-ordintes of cursor to display the tool tip
ameba, if you read the thread, you will find that I originally suggested the GetCursorPos API... Let msali decide what to use.
Avatar of msali

ASKER

Folks... my job for the time being is done... thanx a lot... will get back if need further help.... later for now...

msali.
Thanks for the points! Glad I could help!


Cheers!
Avatar of msali

ASKER

In order to make my life simple the only thing i added was that i set my tool tip's control to my rtb container and it is working just fine....

Final solution includes
GetCaretPos

and since it returs the coordinates in propotion to rtb 0,0 origin i issued the command like follows

set Control.container=rtb.container

and boooooom..... it works

thanx
msa.
Thanks for the update.  Good-luck!

Cheers!