[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2004-08-30
8
Medium Priority
?
260 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
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 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 150 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
Suggested Courses

650 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