check the PreviewKeyDown event of window and do some thing like this
if (e.KeyCode == Keys.NumPad0 || e.KeyCode == Keys.NumPad1)
{
// Do what you want to do.
}
check the PreviewKeyDown event of window and do some thing like this
if (e.KeyCode == Keys.NumPad0 || e.KeyCode == Keys.NumPad1)
{
// Do what you want to do.
}
You could add the same event handler for all buttons with the same code/functionality, like:
buttonZero.Click += numberButton_Click;
buttonOne.Click += numberButton_Click;
...
buttonPlus.Click += numberButton_Click;
buttonMinus.Click += numberButton_Click;
...
private void numberButton_Click(object sender, EventArgs e)
{
checkifequa();
var numButton = sender as Button;
if(numButton != null)
textBox1.Text = textBox1.Text + numButton.Text; // supposing your num buttons have only the number as text (otherwise you could use the Tag property of buttons)
}
private void operatorButton_Click(object sender, EventArgs e)
{
checkifequa();
var operatorButton = sender as Button;
if(operatorButton != null)
textBox1.Text = textBox1.Text + operatorButton .Text; // supposing your operator buttons have only the operator as text (otherwise you could use the Tag property of button)
}
// ...
For the keyboard events, as suggested by cre-johnny07: you could handle previewKeyDown event or KeyDown event and do something like:
private void parentControl_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.NumPad0)
this.buttonZero.PerformClick();
else if (e.KeyCode == Keys.NumPad1)
this.buttonOne.PerformClick();
// and so on...
}
In this way, you will also have a nice button-click effect when typing on the numPad...
What you could also do is make both the mouse clicking event and the keydown event point to the same thing. If you go in your control properties under events, select the same event for both keydown and mouseclick. Just add an "if" statement in your code to check who's calling it.
From my experience, the structure of the program becomes way more neater.
I would take the "business logic" out of the click/key-down handler. Also, I would store the data in private properties instead of the Tag of the TextBox. I've left out the validation code which you normally would have.
private string FirstNumber { get; set; }
private string SecondNumber { get; set; }
private bool IsSecondNumberBeingEntered { get; set; }
private Operation SelectedOperation { get; set;}
private enum Operation
{
Add,
Subtract,
Multiply,
Divide
}
private void button0_Click(object sender, EventArgs e)
{
this.AddNumber(0);
textBox1.Text = textBox1.Text + "0";
}
// Etc.
// In PreviewKeyDown:
if (e.KeyCode == Keys.NumPad0 || e.KeyCode == Keys.D0)
{
this.AddNumber(0);
}
private void AddNumber(int number)
{
if (IsSecondNumberBeingEntered)
{
SecondNumber += number.ToString();
}
else
{
FirstNumber += number.ToString();
}
}
private decimal Calculate()
{
switch (SelectedOperation)
{
case Operation.Add:
return Convert.ToDecimal(FirstNumber) + Convert.ToDecimal(SecondNumber);
case Operation.Subtract:
return Convert.ToDecimal(FirstNumber) - Convert.ToDecimal(SecondNumber);
// etc.
default:
throw new ArgumentOutOfRangeException();
}
}