We help IT Professionals succeed at work.
Get Started

button to follow RichTextBox caret vb.net 2010

596 Views
Last Modified: 2015-03-01
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
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015
Commented:
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE