Solved

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

Posted on 2004-08-30
8
239 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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
 

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

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…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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 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…

803 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