Jump to content

Basic C#

Guest
Go to solution Solved by Helius,

You need to add an "OnKeyDown" event and check which key is being pressed.

 

Here's an example:

        //This event will be fired when a key is being pressed        private void ControlName_KeyDown(object sender, KeyEventArgs e)        {            //If the pressed key is the 1 key, show a msgbox.            if (e.KeyCode == Keys.D1)            {                MsgBox("Test");            }        }

I think you should have a look at this: http://www.dotnetperls.com/keycode

This is code for a basic calculator programmed in C#. It is essentially completed except for the fact i cannot use the keyboard to input information so i need to know how to do that. This is only my first week of C# so a brief explanation of what the code does would be nice. 

 

Thank Davsmith4

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace WindowsFormsApplication2{    public partial class Form1 : Form    {        string input = string.Empty;        string operand1 = string.Empty;        string operand2 = string.Empty;        char operation;        double result = 0.0;        public Form1()        {            InitializeComponent();        }        private void Form1_Load(object sender, EventArgs e)        {        }        private void textBox1_TextChanged(object sender, EventArgs e)        {        }        private void button1_Click(object sender, EventArgs e)        {            this.textBox1.Text = "";            input += "1";            this.textBox1.Text += input;        }        private void button2_Click(object sender, EventArgs e)        {            this.textBox1.Text = "";            input += "2";            this.textBox1.Text += input;        }        private void button3_Click(object sender, EventArgs e)        {            this.textBox1.Text = "";            input += "3";            this.textBox1.Text += input;        }        private void button4_Click(object sender, EventArgs e)        {            this.textBox1.Text = "";            input += "4";            this.textBox1.Text += input;        }        private void button5_Click(object sender, EventArgs e)        {            this.textBox1.Text = "";            input += "5";            this.textBox1.Text += input;        }        private void button6_Click(object sender, EventArgs e)        {            this.textBox1.Text = "";            input += "6";            this.textBox1.Text += input;        }        private void button7_Click(object sender, EventArgs e)        {            this.textBox1.Text = "";            input += "7";            this.textBox1.Text += input;        }        private void button8_Click(object sender, EventArgs e)        {            this.textBox1.Text = "";            input += "8";            this.textBox1.Text += input;        }        private void button9_Click(object sender, EventArgs e)        {            this.textBox1.Text = "";             input += "9";            this.textBox1.Text += input;        }        private void button10_Click(object sender, EventArgs e)        {            operand2 = input;            double num1, num2;            double.TryParse(operand1, out num1);            double.TryParse(operand2, out num2);            this.textBox1.Text = "";            this.input = string.Empty;            this.operand1 = string.Empty;            this.operand2 = string.Empty;            if (operation == '+')            {                result = num1 + num2;                textBox1.Text = result.ToString();            }            else if (operation == '-')            {                result = num1 - num2;                textBox1.Text = result.ToString();            }            else if (operation == '*')            {                result = num1 * num2;                textBox1.Text = result.ToString();            }            else if (operation == '/')            {                if (num2 != 0)                {                    result = num1 / num2;                    textBox1.Text = result.ToString();                }                else                {                    textBox1.Text = "undefined";                }            }        }                       private void button11_Click(object sender, EventArgs e)        {            this.textBox1.Text = "";            this.input = string.Empty;            this.operand1 = string.Empty;            this.operand2 = string.Empty;        }        private void button12_Click(object sender, EventArgs e)        {            this.textBox1.Text = "";            input += "0";            this.textBox1.Text += input;        }        private void button13_Click(object sender, EventArgs e)        {            this.textBox1.Text = "";            operand1 = input;            operation = '*';            input = string.Empty;            this.textBox1.Text += input;        }        private void button14_Click(object sender, EventArgs e)        {            this.textBox1.Text = "";            operand1 = input;            operation = '+';            input = string.Empty;            this.textBox1.Text += input;                    }        private void button15_Click(object sender, EventArgs e)        {            this.textBox1.Text = "";            operand1 = input;            operation = '-';            input = string.Empty;            this.textBox1.Text += input;        }        private void button16_Click(object sender, EventArgs e)        {            this.textBox1.Text = "";            operand1 = input;            operation = '/';            input = string.Empty;            this.textBox1.Text += input;        }    }}
Link to comment
Share on other sites

Link to post
Share on other sites

What exactly do you want explained? All the code you posted?

no no just need a way to use the keyboard to input and would like a brief explanation as to how the code (code that reads keyboard input) works. I simply posted the program as a reference to what i was doing in case you needed it.

Link to comment
Share on other sites

Link to post
Share on other sites

Looks like so far you've only attached listeners to click events, doubtless there is an analog for keyboard events.

Link to comment
Share on other sites

Link to post
Share on other sites

Looks like so far you've only attached listeners to click events, doubtless there is an analog for keyboard events.

Yes i only have click events. Is there anyway to do a keyboard event?

Link to comment
Share on other sites

Link to post
Share on other sites

Well you first want to create 2 textboxes on your form. Make sure to give them a name you will remember like myBoxInput1 and myBoxInput2. Then you want to go to your calculate button click event which looks like button10 and add the appropriate code. To get the value from the box, all you do is reference the text property of your box like so: myBoxInput1.Text & myBoxInput2.Text, and make sure you have a try parse so that if the user enters text, you can throw back an error.

 

To display a number result on a label, just use the .Text property of your label along with ToString: myLabel.Text = myNum.ToString();

 

Edit: Now that I think of it, I don't think you need ToString when pushing numeric values to form controls. But you can use ToString to format to 2 decimal points so you don't have really long decimals.

Link to comment
Share on other sites

Link to post
Share on other sites

You need to add an "OnKeyDown" event and check which key is being pressed.

 

Here's an example:

        //This event will be fired when a key is being pressed        private void ControlName_KeyDown(object sender, KeyEventArgs e)        {            //If the pressed key is the 1 key, show a msgbox.            if (e.KeyCode == Keys.D1)            {                MsgBox("Test");            }        }

I think you should have a look at this: http://www.dotnetperls.com/keycode

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×