0

I am using Microsoft Visual C# 2010 Express to write a Window Form Application. I am writing a Sudoku program. I have written this program once before and got it working. I lost the source code due to a hard drive failure. I got a grid drawn on the form. I got the mouse event to work. I got key press event to work. I then I added several buttons to the form and got them to work. But then a problem occurs. After I added the buttons and got them working, the key press event stops working. Why is there a conflict between the button event and the key press event?

Here is the Code for a demo program that has the same problem. ‘code’ private void doMouseDown(object sender, MouseEventArgs e) { int i; i = 0; }

    private void DoKeyPress(object sender, KeyPressEventArgs e)\\   This worked until I added
    {
        int i;
        i = 1;
    }

    private void doClickButton(object sender, EventArgs e)   \\   This
    {
        int i;
        i = 2;
    }

‘code’

The mouse event and keypress event was added to the form.

Looks like I need a way to set the focus on the form. The program will need to go back and forth between the mouse and the keyboard before the button is used.

Daniel Robert Webb
  • 109
  • 1
  • 1
  • 5
  • 4
    Please can you show us code for those events in order to help you – HatSoft Aug 29 '12 at 18:42
  • What did you add the key event to? The form? Have you verified that the events are still there and that you didn't accidentally remove them? – Thelonias Aug 29 '12 at 18:45
  • I would guess, the object, you added the KeyPress event handlers to, does not have the keyboard focus any more. Maybe PreviewKeyDown will help you. – Nico Schertler Aug 29 '12 at 18:48
  • 1
    take a look at this : http://stackoverflow.com/questions/5499463/fire-form-keypress-event – SidAhmed Aug 29 '12 at 18:50

1 Answers1

0

Set your forms KeyPreview property to true

this.KeyPreview = true;
coolmine
  • 4,427
  • 2
  • 33
  • 45