Welcome to Dream.In.Code
Getting VB Help is Easy!

Join 105,415 VB Programmers for FREE! Ask your question and get quick answers from experts. There are 1,819 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



How to use ESC key to clear text field

 
Reply to this topicStart new topic

How to use ESC key to clear text field

dora
post 3 Jul, 2008 - 02:21 AM
Post #1


New D.I.C Head

*
Joined: 2 Jul, 2008
Posts: 4

hello everyone. I'm new in programming and I'm starting to learn visual basic. I am trying to do some exercises which I found in the internet. A form must have a textbox for input and the result must be into a label in a different font. The textbox and label can be cleared by pressing ESC key. This was what I did

CODE

Private Sub cmdPrint_Click()
lblOutput.Caption = txtInput.Text

End Sub

Private Sub Form_keypress(keyascii As Integer)
If keyascii = Chr() Then
Form1.Refresh
End If
End Sub


I don't know what is the ASCII code for ESC key and how it should be done. Any help will be appreciated. Thank You.
User is offlineProfile CardPM

Go to the top of the page


Zhalix
post 3 Jul, 2008 - 02:32 AM
Post #2


D.I.C Head

**
Joined: 7 May, 2008
Posts: 208



Thanked 8 times
My Contributions


An easy way to get the keyascii for a key is to do something like this:

CODE

Private Sub Form_KeyPress(KeyAscii As Integer)

    MsgBox KeyAscii

End Sub


When you press a key on the keyboard, it pops up a messagebox telling you what the keyascii of it is.

I went ahead and found out that the keyascii of the ESC key is 27.


You wanted to know how to clear the textbox and label when you press the ESC key. Well, first, you gotta make sure you're coding into the textbox control. The textbox will have focus, not the form, and so the textbox will get the keypress event, not the form.

Here's a quick alteration that should do what you need:
CODE

Private Sub Text1_KeyPress(KeyAscii As Integer)

    If KeyAscii = 27 Then
        Text1.Text = ""
        Label1.Caption = ""
    End If

End Sub


I could have written "vbKeyEscape" in place of "27", and it would have worked just as well. It's your choice if you wish to write the number or the words. Just make sure it's a real VB constant before you try to use it. A good way is to type it out in all lowercase and see if it capitalizes first.

Examples: vbKeyA, vbKeySpace

You could alternatively use "vbNullString" in the place of the blank quotes, but I figure this way is easier to remember.

Now I wasn't quite sure what you needed for the "result" of the textbox and putting it into the label. Could you clarify on that?

This post has been edited by Zhalix: 3 Jul, 2008 - 02:43 AM
User is offlineProfile CardPM

Go to the top of the page

dora
post 3 Jul, 2008 - 08:02 AM
Post #3


New D.I.C Head

*
Joined: 2 Jul, 2008
Posts: 4

Thank you very much Zhalix!

It really helped a lot and I learned something new. Now I'm moving on to the next exercise. Thanks again. biggrin.gif
User is offlineProfile CardPM

Go to the top of the page

Zhalix
post 3 Jul, 2008 - 06:46 PM
Post #4


D.I.C Head

**
Joined: 7 May, 2008
Posts: 208



Thanked 8 times
My Contributions


Good luck.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 8/20/08 05:54AM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month