473,406 Members | 2,208 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Key press in c sharp. What am I doing wrong?

I'm pretty new to c sharp... can't get this going. When the user presses z move my animation. This is what I have coded but nothing happens. What am I doing wrong.

Expand|Select|Wrap|Line Numbers
  1. private void Form1_KeyPress(object sender, KeyPressEventArgs e)
  2. {         
  3.             switch (e.KeyChar) 
  4.             {
  5.                 case 'Z':
  6.                     tank1.TurnBarrelLeft();
  7.                     break;
  8.                 case 'X':
  9.                     tank1.TurnBarrelRight();
  10.                     break;
  11.             }
  12. }.
Feb 12 '10 #1

✓ answered by Curtis Rutland

Well, I would use the KeyDown event, first of all.

Did you assign this method to the event handler, or did you just type/paste it in? What I mean is, did you use the Properties panel on the form editor to add the handler?

If you did, you should put a breakpoint in at your method, and see if it's actually being called.

Anyway, here's how I would handle it myself, to remove ambiguity:
Expand|Select|Wrap|Line Numbers
  1. private void Form1_KeyDown(object sender, KeyEventArgs e)
  2. {
  3.     switch (e.KeyCode)
  4.     {
  5.         case Keys.Z:
  6.             //whatever;
  7.             break;
  8.         case Keys.X:
  9.             //whatever;
  10.             break;
  11.         default:
  12.             //sanity check
  13.             break;
  14.     }
  15. }

4 2301
Curtis Rutland
3,256 Expert 2GB
Well, I would use the KeyDown event, first of all.

Did you assign this method to the event handler, or did you just type/paste it in? What I mean is, did you use the Properties panel on the form editor to add the handler?

If you did, you should put a breakpoint in at your method, and see if it's actually being called.

Anyway, here's how I would handle it myself, to remove ambiguity:
Expand|Select|Wrap|Line Numbers
  1. private void Form1_KeyDown(object sender, KeyEventArgs e)
  2. {
  3.     switch (e.KeyCode)
  4.     {
  5.         case Keys.Z:
  6.             //whatever;
  7.             break;
  8.         case Keys.X:
  9.             //whatever;
  10.             break;
  11.         default:
  12.             //sanity check
  13.             break;
  14.     }
  15. }
Feb 12 '10 #2
tlhintoq
3,525 Expert 2GB
TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.
Feb 12 '10 #3
I used a breakpoint and found out it wasn't executing. I was overriding the ProccessCmdKey method ( I thought this was just used for arrow keys ) , so I just popped the code in there and it worked. Thanks for the help
Feb 12 '10 #4
Curtis Rutland
3,256 Expert 2GB
Glad to be of service.
Feb 12 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

50
by: Jerry Sievers | last post by:
<venting> Fellow coders; I just get off the phone today with some clueless headhunter and after listing for her (very proudly I might add) my OS and dev tools platforms of choice; ...
3
by: John Howard | last post by:
I'm trying to write a simple press any key routine, but am having some problems. Here is the routine: void PressAnyKey () { cout << "Press Any Key"; do{}while (!kbhit()); getch(); cout <<...
5
by: InstallAware | last post by:
For Press Inquiries: Sinan Karaca InstallAware Software Corporation 336 Guerrero Street, San Francisco CA 94103 415 358 4094 (voice/fax) sinank@installaware.com http://www.installaware.com/ ...
4
by: Hai Nguyen | last post by:
I'm learning C sharp and do not like vb much. I'm creatiing a wepage using panel to test myself. I tried to use these code below, which is written in VB, and to transform them to c sharp but I got...
1
by: Khan | last post by:
Hi i am doing gps programming but dont know how to start is there any builtin facility avaialable in c sharp or i have to use third party libraries. i need a sample code which can lead me to start...
0
by: Robert Kent | last post by:
We just got our 500th member. Check it out. http://groups.yahoo.com/group/c-sharp-programming/members -- Bill Cousert bill@cousert.com www.cousert.com
23
by: arthur.mcginty | last post by:
Man I've grown tired with C#/.NET the last three days I spent trying to put a NULL value in a database. C# initializes all uninitialized properties to 0 (for numeric types) and didn't have until C#...
1
by: eswanson | last post by:
How do you get the currently logged in user from a regular c sharp class? I have a common utility class in which I have some static functions that called from multiple places. In one of the...
1
by: asif | last post by:
i am new with c sharp.... can u please tell me what is wrong here.... try { //message = _serialPort.ReadLine(); if (_serialPort.ReadLine()== "asif") {
2
complete
by: complete | last post by:
What is the point of using set and get in C Sharp? It seems variables are used differently in this language than in C++. For some reason, you have to have a static variable defined like this:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.