我在编写一个计算器的程序,怎么实现用键盘也能输入数字!

解决方案 »

  1.   

    只输入数字的程序 
    private void OnlyNum_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) 
            { 
                if (!(Char.IsNumber(e.KeyChar) || e.KeyChar == '\b')) 
                { 
                    e.Handled = true; 
                } 
            }
      

  2.   

    是获取键盘输入的数字还是?// Console.ReadLine();
      

  3.   

    using System.Windows.Forms;class Calc : Form
    {
      TextBox text;
      
      Calc()
      {
        text           = new TextBox();
        text.Parent    = this;
        text.Dock      = DockStyle.Fill;
        text.TextAlign = HorizontalAlignment.Right;
        text.ReadOnly  = true;
      }
      
      protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
      {
        switch (keyData)
        {
          case Keys.D0: case Keys.D1: case Keys.D2: case Keys.D3: case Keys.D4:
          case Keys.D5: case Keys.D6: case Keys.D7: case Keys.D8: case Keys.D9:
            text.Text += (char)keyData;
            return true;
        }
        return base.ProcessCmdKey (ref msg, keyData);
      }  static void Main()
      {
        Application.Run(new Calc());
      }
    }
      

  4.   

    using System.Windows.Forms;class Calc : Form
    {
      TextBox text;
      
      Calc()
      {
        text           = new TextBox();
        text.Parent    = this;
        text.Dock      = DockStyle.Top;
        text.TextAlign = HorizontalAlignment.Right;
        text.ReadOnly  = true;
      }
      
      protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
      {
        switch (keyData)
        {
          case Keys.D0: case Keys.D1: case Keys.D2: case Keys.D3: case Keys.D4:
          case Keys.D5: case Keys.D6: case Keys.D7: case Keys.D8: case Keys.D9:
            text.Text += (char)keyData;
            return true;
          case Keys.NumPad0: case Keys.NumPad1: case Keys.NumPad2: case Keys.NumPad3: case Keys.NumPad4:
          case Keys.NumPad5: case Keys.NumPad6: case Keys.NumPad7: case Keys.NumPad8: case Keys.NumPad9:
            text.Text += (char)(keyData - '`' + '0');
            return true;
          case Keys.Back:    // 退格
            if (text.Text.Length > 0) text.Text = text.Text.Substring(0, text.Text.Length - 1);
            return true;
          case Keys.Escape: case Keys.C: // Esc 或 C
            text.Text = null;
            return true;
        }
        return base.ProcessCmdKey (ref msg, keyData);
      }  static void Main()
      {
        Application.Run(new Calc());
      }
    }
      

  5.   

    using System.Windows.Forms;class Calc : Form
    {
      TextBox text;
      
      Calc()
      {
        text           = new TextBox();
        text.Parent    = this;
        text.Dock      = DockStyle.Top;
        text.TextAlign = HorizontalAlignment.Right;
        text.ReadOnly  = true;
      }
      
      protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
      {
        switch (keyData)
        {
          case Keys.D0: case Keys.D1: case Keys.D2: case Keys.D3: case Keys.D4:
          case Keys.D5: case Keys.D6: case Keys.D7: case Keys.D8: case Keys.D9:
            // 大键盘的数字
            text.Text += (char)keyData;
            return true;
          case Keys.NumPad0: case Keys.NumPad1: case Keys.NumPad2: case Keys.NumPad3: case Keys.NumPad4:
          case Keys.NumPad5: case Keys.NumPad6: case Keys.NumPad7: case Keys.NumPad8: case Keys.NumPad9:
            // 小键盘的数字
            text.Text += (char)(keyData - Keys.NumPad0 + '0');
            return true;
          case Keys.OemPeriod: case Keys.Decimal:   // 小数点
            text.Text += '.';
            return true;
          case Keys.Back:                           // 退格
            if (text.Text.Length > 0) text.Text = text.Text.Substring(0, text.Text.Length - 1);
            return true;
          case Keys.Escape: case Keys.C:            // Esc 或 C
            text.Text = null;
            return true;
          case Keys.OemMinus: case Keys.Subtract:   // -
            MessageBox.Show("您按了“-”键!");
            return true;
        }
        return base.ProcessCmdKey (ref msg, keyData);
      }  static void Main()
      {
        Application.Run(new Calc());
      }
    }
      

  6.   

    Keys.OemPeriod: case Keys.Decimal我可找到了,一个大键盘的小数点,一个小键盘的小数点。