Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

RTF cursor Pos

Posted on 1998-04-04
12
Medium Priority
?
370 Views
Last Modified: 2006-11-17
How do u position your cursor in a RTF box
Rather like the LOCATE statement in GWbasic.
I have an app wich needs to scan a RTF box, change the color of certain parts.
0
Comment
Question by:MAVERICK
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +1
12 Comments
 
LVL 9

Expert Comment

by:Dalin
ID: 1460373
MAVERICK,
See if this helps you:

Put this code in the general declare area:

Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
End Type

Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long

Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long


Put the code in where You need to place the mouse pointer to the control:

    Dim MousePos As RECT
    Call GetWindowRect(YourControlNAme.hwnd, MousePos)
    Call SetCursorPos(MousePos.Left, MousePos.Top)

If you have any problems, please let me know.
Regards
Dalin
0
 
LVL 8

Expert Comment

by:mrmick
ID: 1460374
hmmm...

Maverick, I'm not sure I understand the question but if you're trying to set the curser position in a RichTextBox - you can do this using the SelStart property.  For example, the example below moves the curser to the middle of the text.  To see it work, create a new project, add a CommandButton and a RichTextBox.  Add the following code to the CommandButton and run.  Type some text into the RichTextBox and then click the command button...

Private Sub Command1_Click()
 RichTextBox1.SelStart = Len(RichTextBox1.Text) \ 2
 RichTextBox1.SetFocus
End Sub
0
 
LVL 1

Author Comment

by:MAVERICK
ID: 1460375
The dalin exaple comes up with the error "ambiguous name detected: RECT"

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:MAVERICK
ID: 1460376
Can u email me a working example?

BTW mr Mick ur example worked except I really need to set both the line and the column
thanxs for both
0
 
LVL 2

Accepted Solution

by:
marti earned 300 total points
ID: 1460377
Private Sub SetCursor(row As Integer, col As Integer)
    Dim s1 As Integer, s2 As Integer
    Dim i As Integer
   
    s1 = 1
    For i = 1 To row - 1
        s2 = InStr(s1, RichTextBox1.Text, vbCrLf)
        If s2 > s1 Then s1 = s2 + 2
    Next
    RichTextBox1.SelStart = s1 + col - 1
    RichTextBox1.SetFocus

End Sub

Hope this will help.

0
 
LVL 9

Expert Comment

by:Dalin
ID: 1460378
MAVERICK,
 You must have somewhere had another variable or something named RECT.
Try to use Find RECT.
I just tested it again and no error.
Regards
Dalin



0
 
LVL 1

Author Comment

by:MAVERICK
ID: 1460379
I checked for RECT variables in the code and found none!
just incase the problem is something else, could u email the complete project to
tomcat203@geocities.com.

Marti... ur example didn't set the co-ordinates properly!
can u email the example project!


sorry to be pedantic but.............

0
 
LVL 8

Expert Comment

by:mrmick
ID: 1460380
Maverick, marti's answer will not work because s/he's assuming that there is a vbCrLf at the end of each line and when a line wraps, there's no vbCrLf character.

Considering that you stated you wish to "change the color of certain parts" and considering that any search of the text contained in a MultiLine control would return the character offset and not the line and column...

How is it that you can't use my original suggestion?… and how can you use a Col & Row values to manipulate data in the control when this isn’t the way windows locates characters in MultiLine controls?

0
 
LVL 2

Expert Comment

by:marti
ID: 1460381
mrmick, you can always search in variable set to:
sText=RichTextBox1.Text & vbCrLf

MAVERICK I'll send you the working sample.
0
 
LVL 8

Expert Comment

by:mrmick
ID: 1460382
marti, the problem is that lines wrap in a Text/RichText box.  When they do... there's no vbCrLf appended to the text to indicate where a line breaks; therefore, counting lines on this basis doesn't work.  You are, in a sense, counting paragraphs.  Try typing until the text automatically wraps to the next line (don't use the enter key)... and then try positioning the cursor past that line.  The problem with this method will become obvious.
0
 
LVL 2

Expert Comment

by:marti
ID: 1460383
Sounds quite strange but RichTextBox doesn't seem to have wordwrap
0
 
LVL 1

Author Comment

by:MAVERICK
ID: 1460384
Thanks both for the answers!

I have devised a method to solve the problem based on a variation of both answers which works properly!!!!

Incase u wonder why I accepted the answer which was not EXACTLY what I wanted is the suggestion solved the problem anyway!
thanxs both of u

BTW Ancient Mariner had a suggestion for the project which relied upon the Row/Col method!






0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

618 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