Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi i want to show a key value as a message when user press a key.
here function key is not working.
please help me.
VB
Private Sub show_key_val(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
         Handles txt_cust_name.KeyPress, txt_dist.KeyPress
       
MessageBox.Show(Convert.ToInt16(e.KeyChar))
       
    End Sub
Posted
Updated 8-Feb-12 19:56pm
v2
Comments
Sergey Alexandrovich Kryukov 9-Feb-12 1:55am    
What is V.B.NET?
--SA
walterhevedeich 9-Feb-12 1:59am    
Can you really put Int parameters to MessageBox? From what I know, you can only put strings. Maybe that's why its not working.
Sergey Alexandrovich Kryukov 9-Feb-12 2:08am    
Good catch, I did not notices that. But the main problem is that the wrong event is handled.
--SA

1 solution

This event won't help you much. Use another event System.Windows.Forms.Control.KeyDown. It works with different event arguments parameter type, System.Windows.Forms.KeyEventArgs, the one you really can use with functional keys.

Use comparisons like if eventArgs.KeyCode = Keys.F1 …

See:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keydown.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs.keycode.aspx[^] (please see a code sample here).

—SA
 
Share this answer
 
Comments
walterhevedeich 9-Feb-12 2:05am    
Right.
Sergey Alexandrovich Kryukov 9-Feb-12 2:06am    
Thank you,
--SA
Espen Harlinn 9-Feb-12 8:45am    
5'ed!
Sergey Alexandrovich Kryukov 9-Feb-12 11:50am    
Thank you, Espen.
--SA
thatraja 9-Feb-12 11:20am    
Right, 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900