Solved

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

Posted on 2004-08-30
8
234 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
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 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…
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…

861 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

24 Experts available now in Live!

Get 1:1 Help Now