title: c# how to get keydown and button click to do the same thing?
body: <p>I am new to C#.I am learning to make a calculator just like ms windows calculator.the buttons work when i click it, but I want the numpad to work too. Suppose the user types '0', it should be the same as if he clicked the 0 button on my gui. here is my button click event for 0.</p>
  
  <pre><code>private void button0_Click(object sender, EventArgs e)
          {
              checkifequa();
              textBox1.Text = textBox1.Text + "0";
          }
  </code></pre>
  
  <p>how do i get the keydown to work?</p>
  
  <p>EDIT</p>
  
  <p>here is the full source. It was wriiten by some guy on youtube, i am only modifying and trying to learn .Please point any mistakes and also suggest better ways.</p>
  
  <p><pre><code><br>
  using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Windows.Forms;</p>
  
  <p>namespace WindowsFormsApplication1
  {
      public partial class Form1 : Form
      {
          bool plus = false;
          bool minus = false;
          bool into = false;
          bool divd = false;
          bool equa = false;
          public Form1()
          {
              InitializeComponent();
          }</p>
  
      private void button0_Click(object sender, EventArgs e)
      {
          checkifequa();
          textBox1.Text = textBox1.Text + "0";
      }
  
      private void button1_Click(object sender, EventArgs e)
      {
          checkifequa();
  
          textBox1.Text = textBox1.Text + "1";
      }
  
      private void checkifequa()
      {
          if (equa)
              textBox1.Text = "";
          equa = false;
      }
  
      private void button2_Click(object sender, EventArgs e)
      {
          checkifequa();
          textBox1.Text = textBox1.Text + "2";
      }
  
      private void button3_Click(object sender, EventArgs e)
      {
          checkifequa();
          textBox1.Text = textBox1.Text + "3";
      }
  
      private void button4_Click(object sender, EventArgs e)
      {
          checkifequa();
          textBox1.Text = textBox1.Text + "4";
      }
  
      private void button5_Click(object sender, EventArgs e)
      {
          checkifequa();
          textBox1.Text = textBox1.Text + "5";
      }
  
      private void button6_Click(object sender, EventArgs e)
      {
          checkifequa();
          textBox1.Text = textBox1.Text + "6";
      }
  
      private void button7_Click(object sender, EventArgs e)
      {
          checkifequa();
          textBox1.Text = textBox1.Text + "7";
      }
  
      private void button8_Click(object sender, EventArgs e)
      {
          checkifequa();
          textBox1.Text = textBox1.Text + "8";
      }
  
      private void button9_Click(object sender, EventArgs e)
      {
          checkifequa();
          textBox1.Text = textBox1.Text + "9";
      }
  
      private void button11_Click(object sender, EventArgs e)
      {
          checkifequa();
          if(textBox1.Text.Contains("."))
          {
              return;
          }
          else
          {
          textBox1.Text=textBox1.Text+".";
          }
  
      }
  
      private void plusminus_Click(object sender, EventArgs e)
      {
          if (textBox1.Text.Contains("-"))
          {
              textBox1.Text = textBox1.Text.Remove(0, 1);
          }
          else
          {
              textBox1.Text = "-" + textBox1.Text;
          }
      }
  
      private void plus_Click(object sender, EventArgs e)
      {
          if (textBox1.Text == "")
          {
              return;
          }
          else
          { 
              plus = true;
              textBox1.Tag = textBox1.Text;
              textBox1.Text = "";
          }
  
      }
  
      private void equal_Click(object sender, EventArgs e)
      {
          equa = true;
  
          if (plus)
          {
              decimal dec = Convert.ToDecimal(textBox1.Tag) + Convert.ToDecimal(textBox1.Text);
              textBox1.Text = dec.ToString();
  
          }
          if (minus)
          {
              decimal dec = Convert.ToDecimal(textBox1.Tag) - Convert.ToDecimal(textBox1.Text);
              textBox1.Text = dec.ToString();
  
          }
          if (into)
          {
              decimal dec = Convert.ToDecimal(textBox1.Tag) * Convert.ToDecimal(textBox1.Text);
              textBox1.Text = dec.ToString();
  
          }
          if (divd)
          {
              decimal dec = Convert.ToDecimal(textBox1.Tag) / Convert.ToDecimal(textBox1.Text);
              textBox1.Text = dec.ToString();
  
          }
      }
  
      private void substract_Click(object sender, EventArgs e)
      {
  
          if (minus)
              if (textBox1.Text == "")
              {
                  return;
              }
              else
              {
                  minus = true;
                  textBox1.Tag = textBox1.Text;
                  textBox1.Text = "";
              }
      }
  
      private void multiply_Click(object sender, EventArgs e)
      {
          if (textBox1.Text == "")
          {
              return;
          }
          else
          {
              into = true;
              textBox1.Tag = textBox1.Text;
              textBox1.Text = "";
          }
      }
  
      private void divide_Click(object sender, EventArgs e)
      {
          if (textBox1.Text == "")
          {
              return;
          }
          else
          {
              divd = true;
              textBox1.Tag = textBox1.Text;
              textBox1.Text = "";
          }
      }
  
      private void clear_Click(object sender, EventArgs e)
      {
          plus = minus = into = divd = equa = false;
          textBox1.Text = "";
          textBox1.Tag = "";
  
      }
  
  
  
  
      void Form1_KeyPress(object sender, KeyPressEventArgs e)
      {
          if (e.KeyChar &gt;= 48 &amp;&amp; e.KeyChar &lt;= 57)
          {
              switch (e.KeyChar)
              {
                  case (char)48:
                      button0.PerformClick();
                      break;
  
                  case (char)49:
                      button1.PerformClick();
                      break;
                  case (char)50:
                      button2.PerformClick();
                      break;
                  case (char)51:
                      button3.PerformClick();
                      break;
                  case (char)52:
                      button4.PerformClick();
                      break;
                  case (char)53:
                      button5.PerformClick();
                      break;
                  case (char)54:
                      button6.PerformClick();
                      break;
                  case (char)55:
                      button7.PerformClick();
                      break;
                  case (char)56:
                      button8.PerformClick();
                      break;
                  case (char)57:
                      button9.PerformClick();
                      break;
  
              }
  
          }  
      }
  
  
  }
  </code></pre>
  
  <p>}</p>