5 Replies - 6846 Views - Last Post: 18 October 2012 - 10:02 PM Rate Topic: -----

#1 dora  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-July 08

How to use ESC key to clear text field

Posted 03 July 2008 - 02:21 AM

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

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.

Is This A Good Question/Topic? 0
  • +

Replies To: How to use ESC key to clear text field

#2 Zhalix  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 260
  • Joined: 07-May 08

Re: How to use ESC key to clear text field

Posted 03 July 2008 - 02:32 AM

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

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:
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: 03 July 2008 - 02:43 AM

Was This Post Helpful? 0
  • +
  • -

#3 dora  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-July 08

Re: How to use ESC key to clear text field

Posted 03 July 2008 - 08:02 AM

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. :D
Was This Post Helpful? 0
  • +
  • -

#4 Zhalix  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 260
  • Joined: 07-May 08

Re: How to use ESC key to clear text field

Posted 03 July 2008 - 06:46 PM

Good luck.
Was This Post Helpful? 0
  • +
  • -

#5 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 299
  • View blog
  • Posts: 1,755
  • Joined: 26-March 09

Re: How to use ESC key to clear text field

Posted 06 October 2012 - 02:04 AM

View PostZhalix, on 03 July 2008 - 10:32 AM, said:

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

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:
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?


Nice explanation Zhalix...

Technically, you could have the event on the form, and set the KeyPreview property of the form to true, this way the form sees the input before the control, even if the Textbox is focused (if I remember rightly). It really depends at what level you want to capture it.
Was This Post Helpful? 0
  • +
  • -

#6 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 571
  • View blog
  • Posts: 2,979
  • Joined: 19-May 09

Re: How to use ESC key to clear text field

Posted 18 October 2012 - 10:02 PM

You remember rightly. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1