Wrox Programmer Forums
|
Beginning VB 6 For coders who are new to Visual Basic, working in VB version 6 (not .NET).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 5th, 2006, 11:40 AM
Authorized User
 
Join Date: Jul 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default Key press problem

Hi there, i have a bit of a problem with this. I used this

Code:
Private Sub Form_KeyPress(KeyAscii As Integer)
   Char = Chr(KeyAscii)
   If Char = 27 Then Unload Me
End Sub
in my form because i want to unload it when the user press the "esc" key; i have used it in a form and it worked, but now i have a combo box(i preselected an element of it by using text property ) on it and nothing happens.
thanks in advance



 
Old October 5th, 2006, 11:43 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

hi there..

did you set the form property "key preview" to true???

that property catch every key stroke before it send to the control that has the focus.

HTH

Gonzalo
 
Old October 5th, 2006, 12:18 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

You've got this all mixed up.
Let's take a capital ‘A’ for example.
KeyAscii into the KeyPress Sub would be 65.
This is the same as the return value of Asc("A").
Chr(KeyAscii) would return the String “A”. (Chr() returns strings...)

There is no string for the escape key, so I don't really know what the value of Char is in your routine.

You already have the ASCII value when you enter the Sub. So just use
Code:
Private Sub Form_KeyPress(KeyAscii As Integer)
   If KeyAscii = 27 Then Unload Me
End Sub
I'm not certain that Unload Me will work (a quick experiment would confirm or deny), but I'm sure about the ASCII conversion mixup.
 
Old October 5th, 2006, 02:10 PM
Authorized User
 
Join Date: Jul 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hey there, thanks for the help, both of you were right.
but the weird thing is did it the way i posted it and it worked... :P
but when i set the keypreview property to true it did give an error because of the type mismatch.






Similar Threads
Thread Thread Starter Forum Replies Last Post
adding a key press to a batch file jayae Forum and Wrox.com Feedback 0 February 28th, 2008 03:34 PM
Simulating a key press with c++ Bourne C++ Programming 2 April 13th, 2007 02:03 PM
TAB KEY working together KEY PRESS event thomaz C# 4 August 20th, 2006 02:47 PM
key press event in webforms agnihotrived ADO.NET 4 March 19th, 2005 03:22 PM
Force a Key Press? ashley_y VB How-To 2 January 9th, 2004 03:59 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.