Results 1 to 3 of 3

Thread: [RESOLVED] how to disable backspace, space and enter rtb

  1. #1

    Thread Starter
    Banned
    Join Date
    Aug 2016
    Location
    https://t.me/pump_upp
    Posts
    30

    Resolved [RESOLVED] how to disable backspace, space and enter rtb

    hi all.
    i have a richtextbox1 and i want to prevent the user from :
    pressing double space (i.e. user should not allow to press space-bar key twice)
    pressing backspace after space key (backspace will work only within letters and will disable after pressing spacebar)
    prevent user from pressing enter key.

    Earlier in vb6 i was using the following code to disable backspace after space:

    Code:
        If Len(Trim(text1.Text)) > 0 Then
            If KeyAscii = 8 Then
                If Asc(Right(text1.Text, 1)) = 32 Then
                    KeyAscii = 0
                Else
                    KeyAscii = KeyAscii
                End If
            End If
        End If
    How can i do the same in vb.net.
    Early help will be appreciated. Thanks in advance.

  2. #2
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: how to disable backspace, space and enter rtb

    Maybe something like,

    Code:
    Private Sub RichTextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles RichTextBox1.KeyDown
    
        If RichTextBox1.Text.Trim.Length > 0 Then
            If e.KeyCode = Keys.Back OrElse e.KeyCode = Keys.Space Then
                ' get previous char from current position.
                Dim charPos = RichTextBox1.GetPositionFromCharIndex(RichTextBox1.SelectionStart)
                Dim previousChar = RichTextBox1.GetCharFromPosition(charPos)
                ' if previous char is space ignore the keypress.
                If previousChar = " " Then e.SuppressKeyPress = True
            End If
        End If
    
    End Sub

  3. #3

    Thread Starter
    Banned
    Join Date
    Aug 2016
    Location
    https://t.me/pump_upp
    Posts
    30

    Re: how to disable backspace, space and enter rtb

    Thanks Edgemeal ....
    your code is working perfectly. However, i added following line to prevent from pressing Enter in rtb1 (of course idea is yours)
    Code:
      If e.KeyCode = Keys.Enter Then e.SuppressKeyPress = True
    Thanks again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width