-
Jul 26th, 2017, 01:01 PM
#1
Thread Starter
Banned
[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.
-
Jul 26th, 2017, 03:23 PM
#2
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
-
Jul 26th, 2017, 10:34 PM
#3
Thread Starter
Banned
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|