|
-
Mar 16th, 2009, 04:43 PM
#1
Thread Starter
Lively Member
Keyboard Input
Hi, I've got a way of using keyboard input to what I want, But, it's very long. I'll post a snippet of what I've got, and I'd like to if anyone has a better way of doing it.
Code:
public sub frm_KeyPress(KeyAscii as integer)
If KeyAscii = 97 Then
frm1.msg_input.Caption = frm1.msg_input.Caption + "a"
ElseIf KeyAscii = 98 Then
frm1.msg_input.Caption = frm1.msg_input.Caption + "b"
ElseIf KeyAscii = 99 Then
frm1.msg_input.Caption = frm1.msg_input.Caption + "c"
ElseIf KeyAscii = 100 Then
frm1.msg_input.Caption = frm1.msg_input.Caption + "d"
ElseIf KeyAscii = 101 Then
frm1.msg_input.Caption = frm1.msg_input.Caption + "e"
ElseIf KeyAscii = 102 Then
frm1.msg_input.Caption = frm1.msg_input.Caption + "f"
ElseIf KeyAscii = 103 Then
frm1.msg_input.Caption = frm1.msg_input.Caption + "g"
ElseIf KeyAscii = 104 Then
frm1.msg_input.Caption = frm1.msg_input.Caption + "h"
ElseIf KeyAscii = 105 Then
frm1.msg_input.Caption = frm1.msg_input.Caption + "i"
ElseIf KeyAscii = 106 Then
frm1.msg_input.Caption = frm1.msg_input.Caption + "j"
End If
end sub
To me, that seems like a long way of going about it, but whenever I tried:
Code:
frm1.msg_input.Caption = frm1.msg_input.Caption + KeyAscii
I got an error for putting an integer in the string. Any ideas?
Syrillia
-
Mar 16th, 2009, 04:57 PM
#2
Re: Keyboard Input
It would be:
frm1.msg_input.Caption = frm1.msg_input.Caption & Chr$( KeyAscii )
You can use the Chr$(?) to retrieve the ascii character that the KeyAscii value is represents for you.
-
Mar 16th, 2009, 05:02 PM
#3
Thread Starter
Lively Member
Re: Keyboard Input
Works perfect. Ahhh...the change from '+' to '&', I never even suspects that would cause an error. They read the same to me. Just one to clarify then, is '&' the same as 'And'?
-
Mar 16th, 2009, 05:49 PM
#4
Re: Keyboard Input
No... & is for string concatenation (ie: "join these strings together"), and + is to add numbers together.
If you use + on two strings they will be concatenated - but if you use it on a string and a number, VB will try to convert the string to a number (so it can add them), and if the conversion isn't possible you will get a "Data Type Mismatch" error.
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
|