Jump to content

KeyPress won't trigger

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
11 replies to this topic

#1
anotheruser

anotheruser

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
I'm writing an application that can solve Sudoku puzzles. I'm using a Bitmap to represent the grid visually. When I have a Cell selected, pressing a button should change its content. For some reason, the method I wrote won't trigger at all. Selecting stuff by clicking triggers just fine, but this method won't. Why?

#2
Alex3k

Alex3k

    Programmer

  • Members
  • PipPipPipPip
  • 109 posts
Hey mate,

I have a idea why, is it becuase your bitmap is covering the text?

If you could can you put your project in a zip folder and attach it, it will allow me and others to have a look and see from out perspective..
Cheers

Alex
Dreamspark really does "Spark Dreams"
---------------------------------------
View my Blog

#3
anotheruser

anotheruser

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts

Alex3k said:

Hey mate,

I have a idea why, is it becuase your bitmap is covering the text?

THere is no text for the bitmap to cover. The bitmap is the entire thing (except for a "start solving" button in the corner).

#4
anotheruser

anotheruser

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
Here is some sourcecode, if that helps:

   this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Form1_KeyPress);
from Form1.InitializeComponent(). This was automatically generated and I don't understand why it won't work.

The function Form1_KeyPress was written by me. It doesn't even get called.

#5
Alex3k

Alex3k

    Programmer

  • Members
  • PipPipPipPip
  • 109 posts
Hmmmm, When i have always done keypress i have done it this way:
-----------------------------
At the code window there are two drop down boxes, in the first one click form1 events
in the second click keypress
and then your code inside that..

Hope that helps?
Dreamspark really does "Spark Dreams"
---------------------------------------
View my Blog

#6
anotheruser

anotheruser

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
That's what I did. I really can't explain to myself why this doesn't work. The other two events I use, Form1_MouseClick and Form1_Paint, work just fine, but this one won't. Is this a known bug or something?

PS: I HATE interfaces. Why do they make more problems than the code I write myself? Why can I get the program to solve a sudoku puzzle but can't convince the computer to accept my input?:cursing:

#7
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
If you're adding a keypress handler to a form and want it to catch key presses even when a control on that form have focus you have to change KeyPreview of that form to True.

If you are trying to catch the keypresses of the arrow keys it won't work since they are also used to navigate through the controls of the form, to solve this set TabStop to False for all the controls of the form.

Hope this helps you.

Edited by Vswe, 14 August 2010 - 01:40 PM.


#8
anotheruser

anotheruser

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
That first tip finally fixed it. Now I just wonder why this isn't mentioned anywhere. It seems like a problem that would occur more frequently, but I couldn't find anything with google.

Thanks a lot.


edit: I just decided to implement control through the arrow keys as well, but your suggestion how to enable them doesn't work. Is there anything else I need to do? I set the value to false for all controls in the Form. Do I have to change sth. on the Form itself?

Edited by anotheruser, 14 August 2010 - 01:15 PM.


#9
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
Yeah it's kinda strange it's not mentioned often, I remember I've had the problem more than once in the past. To your new problem, I'm sorry but I don't know anything more to do than setting all Tabstops to False. It should work. But can you see anything happen when you press on any of the arrow keys? Like a new control receiving focus or anything?

#10
anotheruser

anotheruser

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
yes, the controls switch focus. Something they shouldn't do, as I set TabStop to false for all of them.

#11
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
what kind of controls do you have?

#12
anotheruser

anotheruser

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
one TextBox that tells the user if an error occured and two Buttons to solve and reset the puzzle. Other than that, there is only the Bitmap that represents the Sudoku grid and its content.