Solved

Marking a line (row) in RichTextBox - Mouse/Keypress Event

Posted on 2004-08-30
8
249 Views
Last Modified: 2013-12-25
Does anyone know how to mark a line (row) in a RichTextBox.  Like a marker bar that moves along with the cursor or up/down keys.  I'm using VB6.0 Enterprise Edition

Thank you
0
Comment
Question by:COOMET
[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
  • 3
  • 3
  • 2
8 Comments
 
LVL 18

Expert Comment

by:JR2003
ID: 11937510
You could do this easily if you use a ListView instead of a RichTextBox.
0
 

Author Comment

by:COOMET
ID: 11949844
True.  The problem is that I'm required to use RichTextBox
0
 
LVL 18

Expert Comment

by:JR2003
ID: 11950047
Requirements are there to be broken! Or at least bent a bit. Is there any particular reason you can't use as listview?
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 11951939
Does the marker need to be inside the RTB?  Perhaps you can draw one outside?'

Idle_Mind
0
 

Author Comment

by:COOMET
ID: 11952921
Ha ha, yes JR2003, requirements can be broken.  The fact is that, my application is getting bulky and am afraid that it may take me a lot of time to get it done in Listview.  I am not so good in VB if something goes wrong in the way of replacing.

Idle_Mind, I wish I could see it marking inside the RTB.  How do you do it ouside the RTB ?  I tried drawing a line that follow the cursor movement but it is not perfect.  It depends on the resolution, etc...

Thanks,
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 11977435
Here is how to draw a line outside the RTB.  The project has only a timer control and a RTB:

Regards,

{pseudocode} / {Time} ± ¼*ƒ(Me.Thoughts÷3)^² = Idle »(°_°)« Mind



Option Explicit

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

Private Type POINTAPI
    x As Long
    y As Long
End Type

Private Sub Form_Load()
    Me.ScaleMode = vbPixels ' Form scalemode must be in pixels for this to work
    ' otherwise, you have to convert the values returned by GetCaretPos() from
    ' client pixels to twips
    Timer1.Interval = 50 ' increase or decrease to your liking...
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
    ' we only clear the background or redraw the line if it has moved...
    Static lastX As Long
    Static lastY As Long
    Dim caretPos As POINTAPI
    GetCaretPos caretPos
    If caretPos.x <> lastX Or caretPos.y <> lastY Then
        ' it is possible for the caret to not be visible if you use the scrollbars
        Me.Cls ' caret position has changed, erase old line
        ' draw new line only if the caret is visible in the rtb at this time
        If (RichTextBox1.Top + caretPos.y) >= RichTextBox1.Top And (RichTextBox1.Top + caretPos.y) <= RichTextBox1.Top + RichTextBox1.Height Then
            Me.Line (RichTextBox1.Left - 30, RichTextBox1.Top + caretPos.y)-(RichTextBox1.Left - 5, RichTextBox1.Top + caretPos.y), vbBlack
            ' store the position so we will know when it has changed
            lastX = caretPos.x
            lastY = caretPos.y
        End If
    End If
End Sub
0
 

Author Comment

by:COOMET
ID: 12030404
Idle_Mind,

How do you change the position of the line with respect to the position of the caret?  In your code, the line is positioned at the top of the caret.  Want to postion it at the middle or bottom of the caret.
Do you have some more ideas on that?

I noticed that the timer is better than with the mouse event.

Thanks,
Cmt
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 50 total points
ID: 12030928
Something like this should work IF EVERYTHING IN THE RTB IS IN THE SAME FONT:

Private Sub Timer1_Timer()
    ' we only clear the background or redraw the line if it has moved...
    Static lastX As Long
    Static lastY As Long
    Dim caretPos As POINTAPI
    GetCaretPos caretPos
    If caretPos.x <> lastX Or caretPos.y <> lastY Then
        ' it is possible for the caret to not be visible if you use the scrollbars
        Me.Cls ' caret position has changed, erase old line
        ' draw new line only if the caret is visible in the rtb at this time
        If (RichTextBox1.Top + caretPos.y) >= RichTextBox1.Top And (RichTextBox1.Top + caretPos.y) <= RichTextBox1.Top + RichTextBox1.Height Then

            Dim h As Single
            Set Me.Font = RichTextBox1.Font
            h = Me.TextHeight(" ")
                   
            Me.Line (RichTextBox1.Left - 30, RichTextBox1.Top + caretPos.y + (h / 2))-(RichTextBox1.Left - 5, RichTextBox1.Top + caretPos.y + (h / 2)), vbBlack

            ' store the position so we will know when it has changed
            lastX = caretPos.x
            lastY = caretPos.y
        End If
    End If
End Sub
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
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…

739 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