We help IT Professionals succeed at work.

button to follow RichTextBox caret vb.net 2010

Hi,

I'm trying to write a winform app with RTB & a Button. The idea is...
*. Button is next to RTB: see screen shot for reference!
1. type text into RTB;
2. press enter key - carriage return but the button [which only moves up or down] moves to same line as cursor/caret.
(IS THERE ANY LOGIC TO THIS - I HEAR YOU ASK YOURSELF.... ONLY TO ME!)
3. If I press BACKSPACE key, delete text until reaching the left-margin.... if move up to next line, button moves up too.
4. if button reaches bottom of RTB... scroll lines will cont in RTB but the button must adhear to the RTB,

I'm still learning vb and need a little help from another pair of eyes.


here is my shocking attempt @ code (sorry).

 
  Private Sub RichTextBox1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles RichTextBox1.KeyDown
        Dim posInLine As Integer = RichTextBox1.SelectionStart - RichTextBox1.GetFirstCharIndexOfCurrentLine()

             If e.KeyCode = Keys.Enter Then ' PRESS ENTER KEY TO GOTO NEXT LINE
            Button1.Top = Button1.Top + 13 ' BUTTON FOLLOWS RTB CARET (KIND OF)
            ToolStripLabel2.Text = RichTextBox1.Lines.Length ' DISPLAYS THE LINE NUMBER IN RTB
        End If

        If e.SuppressKeyPress = (e.KeyCode <> Keys.Back And e.KeyCode <> Keys.Delete) Then ' BACKSPACE KEY PRESSED 
            Button1.Top = Button1.Top - 13 ' I NEED THIS TO ONLY MOVE BUTTON UP AFTER THE CURSOR REACHES LEFT MARGIN - THIS BIT DOESN'T WORK VERY WELL 
            ToolStripLabel2.Text = RichTextBox1.Lines.Length
        End If

        If ToolStripLabel2.Text < "1" Then ' IF REACH TOP OF RTB, THEN STOP
            Button1.Top = 40
        ElseIf ToolStripLabel2.Text > "22" Then
            Button1.Top = 316 ' IF REACH BOTTOM OF RTB, THEN STOP -~ THIS BIT
        End If

    End Sub

Open in new window

vbproj.png
Comment
Watch Question

Most Valuable Expert 2011
Top Expert 2015
Commented:
Here's a close attempt at what you're trying:

Public Class Form1
    Private lineHeight As Single

    Private Sub rtbLeader_TextChanged(sender As Object, e As EventArgs) Handles rtbLeader.TextChanged
        Dim lineCount As Integer = Math.Max(Me.rtbLeader.Lines.Length, 1)  ' Treat empty RTB as having one line
        Dim allLinesHeight As Integer = (lineHeight * lineCount)
        Dim centerOfLine As Integer = allLinesHeight - (lineHeight / 2)
        Dim halfButtonHeight As Integer = (Me.btnFollow.Height / 2)
        Dim adjustedVertical As Integer = (centerOfLine - halfButtonHeight)
        Dim maxButtonTop As Integer = (Me.rtbLeader.Bottom - Me.btnFollow.Height)
        Dim minButtonTop As Integer = Math.Max(adjustedVertical, 0)
        Dim calculatedButtonTop As Integer = (Me.rtbLeader.Top + minButtonTop)

        Me.btnFollow.Top = Math.Min(calculatedButtonTop, maxButtonTop)
        Me.lblLines.Text = lineCount.ToString()
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Using graph As Graphics = Me.rtbLeader.CreateGraphics()
            lineHeight = graph.MeasureString("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", Me.rtbLeader.Font).Height
        End Using
    End Sub
End Class

Open in new window


Screenshot
I say "close" because I think the line height calculation isn't exact. You might try fiddling with it, but in practice it seems to be pretty close.

Author

Commented:
Thanks kaufmed - Excellent solution ;)