Community discussion forum

Rich Text Box formating

  • 1 year ago

    Hi, I am writing an prog that will let my friends update a section of my site without knowing anything about programming. I have most of it sorted apart from this. I have a Rich Text Box where they put in the latest inforation and it need it to add <br> and go on to a new line everytime the Enter Key is pressed. So far i am using

     Private Sub RichTextBox1_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then

    RichTextBox1.Text = RichTextBox1.Text + "<BR>" + vbNewLine

    End If

    End Sub

    However this also adds a blank line at the top of the RTB as the Enter key also acts as usual in the RTB. One way around this i think would be to simulate a backspace keypress (dont know how) Also i then need the cursor to be ready to continue typing on the new line created after the <br>

    Any help would be greatly appreciated.

     

  • 1 year ago

    Hi,
    This isn't the cleanest code but it cancels the enterkey and places the <BR> & carriage return.
    Dirty but does the job.....

    Private Sub RichTextBox1_KeyPress(KeyAscii As Integer)
        Dim x As Long
        If KeyAscii = 13 Then
            x = RichTextBox1.SelStart
            RichTextBox1.Text = Mid(RichTextBox1.Text, 1, x) + "<BR>" + vbCrLf + Mid(RichTextBox1.Text, x + 1, Len(RichTextBox1.Text))
            KeyAscii = 0    ' Cancel the keystroke
            RichTextBox1.SelStart = x + 6 ' Move the cursor to the correct position
        End If
    End Sub

Post a reply

Enter your message below

Sign in or Join us (it's free).

Want to stay in touch with what's going on? Follow us on twitter!