|
-
Mar 14th, 2003, 05:00 PM
#1
Thread Starter
Member
KeyDown event
I have a text box. If the text box has text entered previously then I want to discourage the use of the BACKSPACE and ARROW keys thereby preventing the previously entered text from being deleted. New text is OK. It seems to work fine with the arrow keys but not for the BACKSPACE key. The following is my code.
Help.
Private Sub txtComment_KeyDown(KeyCode As Integer, Shift As Integer)
dim strLen as String
dim blnInsert as Boolean
strLen = Len(txtComment.text)
Select Case KeyCode
Case 8
If strLen > 0 Then
'SendKeys "(^Z)"
KeyCode =0
End If
Case 37, 38, 39, 40
If strLen > 0 Then
KeyCode = 0
End If
Case Else
If Not blnInsert Then
If txtComment.Text = "" Then
'txtComment.Text = CStr(Now()) & " Modified By "
Else
txtComment.Text = txtComment.Text & vbCrLf & CStr(Now()) & " Modified By "
End If
txtComment.SelStart = Len(txtComment.Text)
blnInsert = True
End If
End Select
End Sub
-
Mar 14th, 2003, 06:46 PM
#2
Fanatic Member
I'd suggest using the KeyPress event.
check the ascii of the key pressed if it is considered invalid cancel it. The code below is not tested but I have used this type of thing many a time.
Code:
Private Sub txtComment_KeyPress(Keyascii As Integer)
'I think the ascii for backspace is 8 to check uncomment the
'next line and press the backspace key while the text box has focus
'msgbox keyascii
if keyascii = 8 then
'0 will cancel the keypress
keyascii = 0
endif
end sub
best of luck
-
Mar 15th, 2003, 12:51 PM
#3
Lively Member
keep in mind that KeyPress() will fire only after the user releases the key pressed, not while he's holding it.. KeyDown() will fire just when the user put his finger on the key..
-
Mar 17th, 2003, 09:07 AM
#4
Fanatic Member
Is the keycode for backspace the same as the ascii for backspace.
Thinking about it I think the keydown event does't respond to the same keys as keypress.
to test it out put the following in the keydown event
-
Mar 17th, 2003, 09:29 AM
#5
Frenzied Member
...If the text box has text entered previously then I want to discourage the use of the BACKSPACE and ARROW keys thereby preventing the previously entered text from being deleted. New text is OK....
NB: Emphases mine.
Therefore, rather than handling BackSpace, Arrow Keys - Delete, CTRL-X, Right Click Menu - Cut, and all other such combinations, U could code the KeyDown and KeyUp events to handle for any changes and correct/reset the data in the textbox to what is required.
VB Code:
'Variable to hold the existing text
Dim strOldText As String
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
'Assign the text in the text box to our variable
'before it gets changed
strOldText = Text1.Text
End Sub
Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
'Check if the old text remains undisturbed
If InStr(1, Text1.Text, strOldText, vbBinaryCompare) <> 1 Then
MsgBox "You have changed the existing text. " & _
"Only additions are permitted", vbInformation, "Go Back"
'If the old text is disturbed, revert back to the old text
Text1.Text = strOldText
End If
End Sub
Private Sub Command1_Click()
'Do something with the new text
MsgBox StrReverse(Text1.Text)
End Sub
"Brothers, you asked for it."
...Francisco Domingo Carlos Andres Sebastian D'Anconia
-
Mar 18th, 2003, 10:53 AM
#6
Thread Starter
Member
Thanks KayJay. That worked out really well.
-
Mar 18th, 2003, 10:15 PM
#7
Frenzied Member
Ur Welcome Cheers and Good Luck
"Brothers, you asked for it."
...Francisco Domingo Carlos Andres Sebastian D'Anconia
-
Mar 21st, 2003, 11:10 AM
#8
Fanatic Member
Deucy,
The message box appears when I press a key before I release it.
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
MsgBox KeyAscii
End Sub
-
Mar 21st, 2003, 11:37 AM
#9
Fanatic Member
I thought this was dusted.
Kayjay had it in the keyup which is what you want.
you said to kayjay it was ok.
Keypress will show the messagebox as soon as the finger is pressed.
Are you ok now
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
|