Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 132,119 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,994 people online right now. Registration is fast and FREE... Join Now!




keyboard event

 
Reply to this topicStart new topic

keyboard event, calculator using visual c#

javagoutom
post 30 May, 2008 - 10:03 PM
Post #1


New D.I.C Head

*
Joined: 20 Sep, 2007
Posts: 25


My Contributions


Attached File  mast.zip ( 61.79k ) Number of downloads: 20
Hi, m tryn to build a calculator using visual c# 2008.... i built that but cnt catch the keyboard event.. like that of calculator in windows.... os... i need to calculate the answer when the user presses the enter key whereever the focus be and also it should be able to handle the numeric key in keyboard that whenever a user press the key wether in the albhabet sction or in the numeric keypad.... it should directly entr the vlues to the textbox... and also it should highlight the key in the interface that is pressed in the keyboard (like that of the windows calculator)........ how can i do this. help me.. plzzz........

CODE


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;

namespace mast
{
    public partial class Form1 : Form
    {
        String fs = "";
        public Form1()
        {
            InitializeComponent();
            
        }
        
        private void button12_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            label3.Text = "";
            add.Enabled = true;
            mul.Enabled = true;
            sub.Enabled = true;
            mod.Enabled = true;
            div.Enabled = true;
        }

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

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

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

        private void button8_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                textBox1.Text = "4";
            }
            else
                textBox1.AppendText("4");
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                textBox1.Text = "5";
            }
            else
                textBox1.AppendText("5");
        }

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

        private void button16_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                textBox1.Text = "7";
            }
            else
                textBox1.AppendText("7");
        }

        private void button15_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                textBox1.Text = "8";
            }
            else
                textBox1.AppendText("8");
        }

        private void button14_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                textBox1.Text = "9";
            }
            else
                textBox1.AppendText("9");
        }

        private void button11_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                textBox1.Text = "0";
            }
            else
                textBox1.AppendText("0");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            { }
            else if ((textBox1.Text).Contains("+") || (textBox1.Text).Contains("-") || (textBox1.Text).Contains("*") || (textBox1.Text).Contains("/") || (textBox1.Text).Contains("%"))
            {
                                
            }
            else
            {
                fs = textBox1.Text;
                textBox1.AppendText("+");
            }
        }

        private void sub_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            { }
            else if ((textBox1.Text).Contains("+") || (textBox1.Text).Contains("-") || (textBox1.Text).Contains("*") || (textBox1.Text).Contains("/") || (textBox1.Text).Contains("%"))
            {
            
            }
            else
            {
                fs = textBox1.Text;
                textBox1.AppendText("-");
            }
        }

        private void mul_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            { }
            else if ((textBox1.Text).Contains("+") || (textBox1.Text).Contains("-") || (textBox1.Text).Contains("*") || (textBox1.Text).Contains("/") || (textBox1.Text).Contains("%"))
            {
                
            }
            else
            {
                fs = textBox1.Text;
                textBox1.AppendText("*");
            }
        }

        private void div_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            { }
            else if ((textBox1.Text).Contains("+") || (textBox1.Text).Contains("-") || (textBox1.Text).Contains("*") || (textBox1.Text).Contains("/") || (textBox1.Text).Contains("%"))
            {
                
            }
            else
            {
                fs = textBox1.Text;
                textBox1.AppendText("/");
            }
        }

        private void mod_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            { }
            else if ((textBox1.Text).Contains("+") || (textBox1.Text).Contains("-") || (textBox1.Text).Contains("*") || (textBox1.Text).Contains("/") || (textBox1.Text).Contains("%"))
            {
                
            }
            else
            {
                fs = textBox1.Text;
                textBox1.AppendText("%");
            }
        }

        private void button1_MouseEnter(object sender, EventArgs e)
        {

            button1.BackColor = System.Drawing.Color.Black;
            button1.ForeColor = System.Drawing.Color.White;
        }

        private void button1_MouseLeave(object sender, EventArgs e)
        {
            button1.BackColor = System.Drawing.SystemColors.ButtonFace;
            button1.ForeColor = System.Drawing.Color.Black;
        }

        private void button2_MouseEnter(object sender, EventArgs e)
        {
            button2.BackColor = System.Drawing.Color.Black;
            button2.ForeColor = System.Drawing.Color.White;
        }

        private void button2_MouseLeave(object sender, EventArgs e)
        {
            button2.BackColor = System.Drawing.SystemColors.ButtonFace;
            button2.ForeColor = System.Drawing.Color.Black;
        }

        private void button3_MouseEnter(object sender, EventArgs e)
        {
            button3.BackColor = System.Drawing.Color.Black;
            button3.ForeColor = System.Drawing.Color.White;
        }

        private void button3_MouseLeave(object sender, EventArgs e)
        {
            button3.BackColor = System.Drawing.SystemColors.ButtonFace;
            button3.ForeColor = System.Drawing.Color.Black;
        }

        private void button8_MouseEnter(object sender, EventArgs e)
        {
            button8.BackColor = System.Drawing.Color.Black;
            button8.ForeColor = System.Drawing.Color.White;
        }

        private void button8_MouseLeave(object sender, EventArgs e)
        {
            button8.BackColor = System.Drawing.SystemColors.ButtonFace;
            button8.ForeColor = System.Drawing.Color.Black;
        }

        private void button7_MouseEnter(object sender, EventArgs e)
        {
            button7.BackColor = System.Drawing.Color.Black;
            button7.ForeColor = System.Drawing.Color.White;
        }

        private void button7_MouseLeave(object sender, EventArgs e)
        {
            button7.BackColor = System.Drawing.SystemColors.ButtonFace;
            button7.ForeColor = System.Drawing.Color.Black;
        }

        private void button6_MouseEnter(object sender, EventArgs e)
        {
            button6.BackColor = System.Drawing.Color.Black;
            button6.ForeColor = System.Drawing.Color.White;
        }

        private void button6_MouseLeave(object sender, EventArgs e)
        {
            button6.BackColor = System.Drawing.SystemColors.ButtonFace;
            button6.ForeColor = System.Drawing.Color.Black;
        }

        private void button16_MouseEnter(object sender, EventArgs e)
        {
            button16.BackColor = System.Drawing.Color.Black;
            button16.ForeColor = System.Drawing.Color.White;
        }

        private void button16_MouseLeave(object sender, EventArgs e)
        {
            button16.BackColor = System.Drawing.SystemColors.ButtonFace;
            button16.ForeColor = System.Drawing.Color.Black;
        }

        private void button15_MouseEnter(object sender, EventArgs e)
        {
            button15.BackColor = System.Drawing.Color.Black;
            button15.ForeColor = System.Drawing.Color.White;
        }

        private void button15_MouseLeave(object sender, EventArgs e)
        {
            button15.BackColor = System.Drawing.SystemColors.ButtonFace;
            button15.ForeColor = System.Drawing.Color.Black;
        }

        private void button14_MouseEnter(object sender, EventArgs e)
        {
            button14.BackColor = System.Drawing.Color.Black;
            button14.ForeColor = System.Drawing.Color.White;
        }

        private void button14_MouseLeave(object sender, EventArgs e)
        {
            button14.BackColor = System.Drawing.SystemColors.ButtonFace;
            button14.ForeColor = System.Drawing.Color.Black;
        }

        private void button11_MouseEnter(object sender, EventArgs e)
        {
            button11.BackColor = System.Drawing.Color.Black;
            button11.ForeColor = System.Drawing.Color.White;
        }

        private void button11_MouseLeave(object sender, EventArgs e)
        {
            button11.BackColor = System.Drawing.SystemColors.ButtonFace;
            button11.ForeColor = System.Drawing.Color.Black;
        }

        private void eq_Click(object sender, EventArgs e)
        {
            
            String s = textBox1.Text;
            if ((textBox1.Text).EndsWith("+") || (textBox1.Text).EndsWith("-") || (textBox1.Text).EndsWith("*") || (textBox1.Text).EndsWith("/") || (textBox1.Text).EndsWith("%") || (textBox1.Text).EndsWith("^"))
            { }
            else if (s.Contains("+"))
            {
                int indx = s.IndexOf("+");
                String s2 = s.Substring(indx + 1);
                float num1 = Convert.ToSingle(fs);
                float num2 = Convert.ToSingle(s2);
                float num3 = num1 + num2;
                label3.Text = Convert.ToString(num3);
            }

            else if (s.Contains("-"))
            {
                int indx = s.IndexOf("-");
                String s2 = s.Substring(indx + 1);
                float num1 = Convert.ToSingle(fs);
                float num2 = Convert.ToSingle(s2);
                float num3 = num1 - num2;
                label3.Text = Convert.ToString(num3);
            }

            else if (s.Contains("*"))
            {
                int indx = s.IndexOf("*");
                String s2 = s.Substring(indx + 1);
                float num1 = Convert.ToSingle(fs);
                float num2 = Convert.ToSingle(s2);
                float num3 = num1 * num2;
                label3.Text = Convert.ToString(num3);
            }

            else if (s.Contains("/"))
            {
                int indx = s.IndexOf("/");
                String s2 = s.Substring(indx + 1);
                try
                {
                    float num1=Convert.ToSingle(fs);
                    float num2=Convert.ToSingle(s2);
                    float num3 = num1 / num2;
                    label3.Text = Convert.ToString(num3);
                }
                catch (Exception)
                {
                    label3.Text = "Cannot divide by zero";
                }
            }

            else if (s.Contains("%"))
            {
                int indx = s.IndexOf("%");
                String s2 = s.Substring(indx + 1);
                label3.Text = label3.Text = Convert.ToString(Convert.ToInt32(fs) % Convert.ToInt32(s2));;
            }
            else if (s.Contains("^"))
            {
                int indx = s.IndexOf("^");
                String s2 = s.Substring(indx + 1);
                double num1 = Convert.ToDouble(fs);
                double num2 = Convert.ToDouble(s2);
                double num3 = Math.Pow(num1, num2);
                label3.Text = Convert.ToString(num3);
            }
        }

        private void button4_Click_1(object sender, EventArgs e)
        {
            mod.Enabled = false;
            if (textBox1.Text == "")
            {
                textBox1.Text = ".";
            }
            else if ((textBox1.Text).Contains("."))
            {

            }
            else
                textBox1.AppendText(".");
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.TabIndex = 0;
            textBox1.Focus();
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsNumber(e.KeyChar)==false)
            {
                e.Handled = true;
            }
            else
            {}
        }

        private void button5_Click(object sender, EventArgs e)
        {
            float sg=Convert.ToSingle(textBox1.Text);
            
            label3.Text=Convert.ToString(Math.Sqrt(sg));
        }

        private void button9_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            { }
            else if ((textBox1.Text).Contains("+") || (textBox1.Text).Contains("-") || (textBox1.Text).Contains("*") || (textBox1.Text).Contains("/") || (textBox1.Text).Contains("%")||(textBox1.Text).Contains("^"))
            {

            }
            else
            {
                fs = textBox1.Text;
                textBox1.AppendText("^");
            }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            { }
            else if ((textBox1.Text).Contains("+") || (textBox1.Text).Contains("-") || (textBox1.Text).Contains("*") || (textBox1.Text).Contains("/") || (textBox1.Text).Contains("%") || (textBox1.Text).Contains("^"))
            {

            }
            else
            {
                double sqr = Convert.ToDouble(textBox1.Text);
                label3.Text = Convert.ToString(Math.Pow(sqr,2));
            }
        }

        private void button13_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            { }
            else if ((textBox1.Text).Contains("+") || (textBox1.Text).Contains("-") || (textBox1.Text).Contains("*") || (textBox1.Text).Contains("/") || (textBox1.Text).Contains("%") || (textBox1.Text).Contains("^"))
            {

            }
            else
            {
                double sqr = Convert.ToDouble(textBox1.Text);
                label3.Text = Convert.ToString(Math.Pow(sqr, 3));
            }
        }

        private void button17_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            { }
            else if ((textBox1.Text).Contains("+") || (textBox1.Text).Contains("-") || (textBox1.Text).Contains("*") || (textBox1.Text).Contains("/") || (textBox1.Text).Contains("%") || (textBox1.Text).Contains("^"))
            {

            }
            else
            {
                double sqr = Convert.ToDouble(textBox1.Text);
                label3.Text = Convert.ToString(1/sqr);
            }
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                //MessageBox.Show("Enter key pressed...");
                Invoke(new System.EventHandler(eq_Click)); // Invoking other event handler...

            }
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
                if (textBox1.Focused == true)
                {
                    
                        
                            //MessageBox.Show("Enter key pressed...");
                           // Invoke(new System.EventHandler(textBox1_KeyDown)); // Invoking other event handler...

                        
                }

                if (e.KeyData == Keys.T)
                {
                    button2.Focus();
                }
        }


    }
}


User is offlineProfile CardPM

Go to the top of the page

eclipsed4utoo
post 2 Jun, 2008 - 07:30 AM
Post #2


D.I.C Regular

***
Joined: 21 Mar, 2008
Posts: 314



Thanked 17 times
My Contributions


to catch the Enter keypress and numeric keypresses

C#

private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("Enter key was pressed");
}
else if (e.KeyCode == Keys.NumPad1 || e.KeyCode == Keys.D1)
{
MessageBox.Show("Numeric 1 key was pressed");
}
}

User is online!Profile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/21/08 10:41AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month