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
RobertoFreemanoAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RobertoFreemanoAuthor Commented:
Thanks kaufmed - Excellent solution ;)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.