Solved

Rich Text Box Cursor Position

Posted on 2000-02-16
20
687 Views
Last Modified: 2013-12-25
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.
0
Comment
Question by:msali
  • 10
  • 5
  • 5
20 Comments
 
LVL 14

Accepted Solution

by:
mcrider earned 200 total points
ID: 2530023
You can figure out the word by using the SelStart property of the richtextbox.  I would also look into the GetLineFromChar property of the richtextbox.


As for the cursor position, you can get that by using the GetCursorPos API.  It's defined this way:

   Type POINTAPI
        x As Long
        y As Long
   End Type
   Declare Function GetCursorPos Lib "user32" Alias "GetCursorPos" (lpPoint As POINTAPI) As Long


You can then use it this way:

   Dim MouseLoc as POINTAPI
   GetCursorPos MouseLoc

Then, MouseLoc.x and MouseLoc.y are the screen coordinates of the mouse pointer...


Cheers!
0
 
LVL 15

Expert Comment

by:ameba
ID: 2530358
63 / 68
0
 

Author Comment

by:msali
ID: 2533738
thnax, and how about which word my cursor is on in the RTB?
0
 

Author Comment

by:msali
ID: 2533759
i am lost on ameba's answer 63 / 68
are these some kind of code words?
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2533836
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!
0
 

Author Comment

by:msali
ID: 2537696
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...
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2537774
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!





0
 
LVL 15

Expert Comment

by:ameba
ID: 2539251
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:
http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=10102972
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2539296
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...
0
 
LVL 15

Expert Comment

by:ameba
ID: 2539307
>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
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 14

Expert Comment

by:mcrider
ID: 2539327
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.
0
 
LVL 15

Expert Comment

by:ameba
ID: 2539346
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!
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2539360
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

0
 
LVL 14

Expert Comment

by:mcrider
ID: 2539375
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.
0
 
LVL 15

Expert Comment

by:ameba
ID: 2539383
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
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2539419
ameba, if you read the thread, you will find that I originally suggested the GetCursorPos API... Let msali decide what to use.
0
 

Author Comment

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

msali.
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2541795
Thanks for the points! Glad I could help!


Cheers!
0
 

Author Comment

by:msali
ID: 2544688
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.
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2545183
Thanks for the update.  Good-luck!

Cheers!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now