Solved

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

Posted on 2004-08-30
8
232 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
  • 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
 
LVL 85

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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 85

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 85

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

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.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Input past end of file vbs script 9 69
VBA error replacing data 6 35
Determine Range to Select 5 35
Add and format columns in vb6 7 26
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

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

16 Experts available now in Live!

Get 1:1 Help Now