1 Replies - 568 Views - Last Post: 30 September 2013 - 05:07 AM Rate Topic: -----

#1 themlas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 30-September 13

How to add keyboard input to calculator?

Posted 30 September 2013 - 05:01 AM

I am new to c#. I made a calculator. its working so far. But i want to enable keyboard input also. I don't know how to do it. and where to write the code.
This is the code so far...

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

        double num1=0, num2, result;
        string op;
        
        private void button_plus_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "") 
                num1 = Convert.ToDouble(textBox1.Text);

            textBox1.Text = String.Empty;
            op = "+";
            
        }

        private void button_minus_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
                 num1 = Convert.ToDouble(textBox1.Text);

            textBox1.Text = String.Empty;
            op = "-";
            
        }

        private void button_mul_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "") 
                  num1 = Convert.ToDouble(textBox1.Text);

            textBox1.Text = String.Empty;
            op = "*";
            
        }

        private void button_div_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "") 
                     num1 = Convert.ToDouble(textBox1.Text);

            textBox1.Text = String.Empty;
            op = "/";
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + "1";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + "2";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + "3";
        }

        private void button4_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + "4";
        }

        private void button5_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + "5";
        }

        private void button6_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + "6";
        }

        private void button7_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + "7";
        }

        private void button8_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + "8";
        }

        private void button9_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + "9";
        }

        private void button0_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + "0";
        }

        private void button00_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + "00";
        }

        private void button_point_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + ".";
        }

        private void button_clear_Click(object sender, EventArgs e)
        {
            textBox1.Text = String.Empty;
        }

        private void button_result_Click(object sender, EventArgs e)
        {
            calculate(op);

        }
  
        public void calculate( string op)
        {
            num2 = Convert.ToDouble(textBox1.Text);
            switch(op)
            {
                case "+" : result=num1+num2;
                    textBox1.Text = result.ToString(); break;
                case "-": result = num1 - num2;
                    textBox1.Text = result.ToString(); break;
                case "*": result = num1 * num2;
                    textBox1.Text = result.ToString(); break;
                case "/": result = num1 / num2;
                    textBox1.Text = result.ToString(); break;

            }
            num1 = 0; num2 = 0;            
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            
            textBox1.Font = new Font("Arial",12, FontStyle.Bold);
            textBox1.Cursor = Cursors.Arrow;

            
        }

            
    }
}




I am attaching screenshot of calculator.
If i clicked 5 from keyboard I want 5 to be entered in the text field. And if i press + it must invoke "+" in calculator.

Is This A Good Question/Topic? 0
  • +

Replies To: How to add keyboard input to calculator?

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3239
  • View blog
  • Posts: 10,869
  • Joined: 12-December 12

Re: How to add keyboard input to calculator?

Posted 30 September 2013 - 05:07 AM

Investigate Form.KeyPreview

MSDN said:

When this property is set to true, the form will receive all KeyPress, KeyDown, and KeyUp events. After the form's event handlers have completed processing the keystroke, the keystroke is then assigned to the control with focus. For example, if the KeyPreview property is set to true and the currently selected control is a TextBox, after the keystroke is handled by the event handlers of the form the TextBox control will receive the key that was pressed. To handle keyboard events only at the form level and not allow controls to receive keyboard events, set the KeyPressEventArgs.Handled property in your form's KeyPress event handler to true.

You can use this property to process most keystrokes in your application and either handle the keystroke or call the appropriate control to handle the keystroke. For example, when an application uses function keys, you might want to process the keystrokes at the form level rather than writing code for each control that might receive keystroke events.

There is an example in that page.

This post has been edited by andrewsw: 30 September 2013 - 05:08 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1