Home > Net >  Is there a way to handle all the number buttons in one keyboard event instead of doing for each butt
Is there a way to handle all the number buttons in one keyboard event instead of doing for each butt

Time:09-15

Instead of copying and pasting for each number is there a method that could reference to all buttons?

private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.D5)
            {
                Five.PerformClick(); 
                    
            }
        }

        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.D5)
            {
                Five.PerformClick();
            }
        }

CodePudding user response:

Firstly, the enum values Keys.D0 to Keys.D9 have sequential integer values. You can abuse this knowledge to turn the KeyCode directly into an array index.

var buttons = new Button[] {Zero, One, ... etc ...};
if(e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9){
    var index = (int)e.KeyCode - (int)Keys.D0;
    var button = buttons[index];
    button.PerformClick();
}
if(e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9){
    // similar to the above
}

Or you could rearrange your code. Create a separate method for doing the work of "user entered a digit". Then call that method from both the button click event and form key event.

private void HandleDigit(int value){
    // todo
}

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if(e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9){
        var value = (int)e.KeyCode - (int)Keys.D0;
        HandleDigit(value);
    }
}

CodePudding user response:

Maybe this can give you an idea -- instead of the code you show something like

 Button.PerformClick('5');

would do what you want. Or even

 Button.PerformNumberClick(5);

and if is preseed

 Button.PerformOperatorClick(' ');

CodePudding user response:

You can create and event for the first button and then tie all the calculator buttons to that event, inside the event you know what key (number) is pressed. Here is a code that could be useful, i only added two buttons to test it and notice when i live the form im releasing handles unsubscribing the events:

using System;
using System.Windows.Forms;

namespace Calculator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            CmdButton1.KeyDown  = CmdButtonKeyDown;
            CmdButton2.KeyDown  = CmdButtonKeyDown;
        }

        private void CmdButtonKeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode==Keys.NumPad1 || e.KeyCode == Keys.NumPad2 )
            MessageBox.Show("KeyPressed is "   e.KeyCode.ToString());
        }

        private void Form1_Leave(object sender, EventArgs e)
        {
            CmdButton1.KeyDown -= CmdButtonKeyDown;
            CmdButton2.KeyDown -= CmdButtonKeyDown;
        }
    }
}
  • Related