1

I have made a basic calculator form in visual studio that is fully functional using the mouse click events. I am trying to add keyboard press events so you can use keyboard or mouse but am having trouble figuring it out.

I added some code for the keypress event but it only works if I mouse click the button first then the keyboard event will work, if I mouse select another number it stops working.

//Code that handles click events
private void number_Click(object sender, EventArgs e)
    {
        if((txtResult.Text == "0")||(operation))
        {
            txtResult.Clear();
        }           
        //Cast button to the object sender
        operation = false;
        Button button = (Button)sender;
        //If the button pressed is a "." then check if the txtResult already contains
        //a ".", if it does contain a "." then do nothing
        if (button.Text == ".")
        {
            if (!txtResult.Text.Contains("."))
            {
                txtResult.Text = txtResult.Text + button.Text;
            }
        }else
        txtResult.Text = txtResult.Text + button.Text;
    }


//Keyboard press event code

    private void num1_key(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == '1')
        {
//If keyboard '1' pressed perform number_click()
            btn1.PerformClick();
            e.Handled = true;
        }
    }

Is there anything glaringly obvious Im missing or am I on the wrong track?

Flater
  • 12,908
  • 4
  • 39
  • 62
  • 1
    Put this on the Window key press – Tony Jun 22 '18 at 22:31
  • Im sorry I dont quite understand, could you elaborate? – saulsebrook Jun 22 '18 at 22:36
  • It sounds like you want to capture the key press events from anywhere on your form. If so, check this question: https://stackoverflow.com/questions/3001237/how-to-catch-a-key-press-on-a-c-sharp-net-form – Rufus L Jun 22 '18 at 22:40
  • 1
    [Form.KeyPreview Property](https://msdn.microsoft.com/en-us/library/system.windows.forms.form.keypreview(v=vs.110).aspx) – Rufus L Jun 22 '18 at 22:42
  • Im thinking i might be too beginner for this, im not understanding still, I added keypreview = true and it still doesnt work. Do you know of any good tutorials to get my head around this? – saulsebrook Jun 22 '18 at 22:54
  • @saulsebrook I have posted the answer below, this might help you. – Mohammad Ajmal Amirzad Jun 22 '18 at 22:56

1 Answers1

2

Updated Answer

As you still have problems, what you have to do is to remove the previous code and go with the below code.

    //Have your form's KeyPress event like this, replace textBox1 with the name of your textbox
    private void Form1_KeyPress(object sender, KeyPressEventArgs e)
    {
        switch (e.KeyChar)
        {
            case '0':
                textBox1.Text += "0";
                break;

            case '1':
                textBox1.Text += "1";
                break;

            case '2':
                textBox1.Text += "2";
                break;

            case '3':
                textBox1.Text += "3";
                break;

            case '4':
                textBox1.Text += "4";
                break;

            case '5':
                textBox1.Text += "5";
                break;

            case '6':
                textBox1.Text += "6";
                break;

            case '7':
                textBox1.Text += "7";
                break;

            case '8':
                textBox1.Text += "8";
                break;

            case '9':
                textBox1.Text += "9";
                break;
            case '.':
                textBox1.Text += ".";
                break;
        }

        e.Handled = true;
    }

Have your form's load event like this.

    private void Form1_Load(object sender, EventArgs e)
    {
        this.KeyPreview = true;
    }

Old Answer

What you have to do is create KeyPress event on your Form just like shown below.

private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == '1')
    {
        btn1_Click(null, null);
    }
    else if (e.KeyChar == '2')
    {
        btn2_Click(null, null);
    }
    //Go write your code with else if.........

}

Then as you would like to capture the KeyPress event wherever the focus is in the form, The below code will add the above KeyPress event to all the controls on your form so you can capture the KeyPress event wherever the focus is in the form.

private void Form1_Load(object sender, EventArgs e)
{
    foreach (Control ctrl in Controls)
    {
        ctrl.KeyPress += Form1_KeyPress;
    }
}

As mentioned by @Rufus L in his answer, you may just use the KeyPreview property which will capture the KeyPress event of other control before the actual control receives with the below code.

private void Form1_Load(object sender, EventArgs e)
{
    this.KeyPreview = true;
}