Numpad Key Codes in C #

I use these following codes to work with numpad keys.

if (e.KeyCode == Keys.NumPad0 || e.KeyCode == Keys.D0)
{
   MessageBox.Show("You have pressed numpad0");
}
if (e.KeyCode == Keys.NumPad1 || e.KeyCode == Keys.D1)
{
   MessageBox.Show("You have pressed numpad1");
}

And also for other numpad keys. But I want to know how I can do this for the "+", "*", "/", "-", ".", Which is located next to the numpad keys.

Thanks in advance

+3
source share
3 answers

For "+", "*", "/" we can use the KeyDown event for "-", ".". we can use the KeyPress event.

Here are the codes:

private void button1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Add)
        {
            MessageBox.Show("You have Pressed '+'");
        }
        else if (e.KeyCode == Keys.Divide)
        {
            MessageBox.Show("You have Pressed '/'");
        }
        else if (e.KeyCode == Keys.Multiply)
        {
            MessageBox.Show("You have Pressed '*'");
        }
    }
private void button1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == '.')
        {
            MessageBox.Show("You have pressed '.'");
        }
        else if (e.KeyChar == '-')
        {
            MessageBox.Show("You have pressed '-'");
        }
    }
+3
source

Browse the entire Keysenum . Do you have Keys.Mutiply, Keys.Addetc.

Please note that Keys.D0it is not number 0, it is not numpad 0.

+9
source

switch .

KeyDown . :

switch (e.KeyCode)
{
    case Keys.NumPad1:
        tbxDisplay.Text = tbxDisplay.Text + "1";
    break;
    case Keys.NumPad2:
        tbxDisplay.Text = tbxDisplay.Text + "2";
    break;
    case Keys.NumPad3:
        tbxDisplay.Text = tbxDisplay.Text + "3";
    break;
}

.

Another thing is that if the user pressed a button on the screen, the focus will be lost from the text field, and key entries will no longer work. But this is easily fixed with .focus () on the buttons.

0
source

All Articles