C # writing a simple calculator

Source: Internet
Author: User

Just in touch with C #, according to convention, write a simple calculator, write only addition, multiplication, other similar, editor's vs2008

First Open VS, Create a new C # Windows Forms application, and the next project name is WindowsFormsApplication2, not WindowsFormsApplication3.

Then design the UI interface of the calculator, it is relatively simple, please understand ...

The next step is coding, which is to add a click event to the button, the code is as follows:

Button1. Click + = new EventHandler (Btns_click);
Button2. Click + = new EventHandler (Btns_click);

But these two lines of code can not be placed in the code alone, need to put in a method inside;

private void Addoperatorbtns ()
{

Button1. Click + = new EventHandler (Btns_click);
Button2. Click + = new EventHandler (Btns_click);

}

You will then declare the method:

private void Form1_Load (object sender, EventArgs e)
{
Addoperatorbtns ();
}

The following is the code implementation of the Click event Method:

private void Btns_click (object sender, EventArgs e)//button Click event
{

Button m_curbtn = (button) sender;
Switch (m_curbtn.name)
{
Case "Button1":
{
A = double. Parse (TextBox1.Text);
b = Double. Parse (TextBox2.Text);
c = a + B;

TextBox3.Text = c+ "";
Break
}
Case "Button2":
{
A = double. Parse (TextBox1.Text);
b = Double. Parse (TextBox2.Text);
c = A * b;

TextBox3.Text = C + "";
Break
}

}

}

All the code in the final Form1.cs is as follows:

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 WindowsFormsApplication2
{
public partial class Form1:form
{

Double A = 0;
Double b = 0;
Double c = 0;
Public Form1 ()
{
InitializeComponent ();
}

private void Form1_Load (object sender, EventArgs e)
{
Addoperatorbtns ();
}

private void Label1_click (object sender, EventArgs e)
{

}

private void Addoperatorbtns ()
{

Button1. Click + = new EventHandler (Btns_click);
Button2. Click + = new EventHandler (Btns_click);

}

private void Btns_click (object sender, EventArgs e)//button Click event
{

Button m_curbtn = (button) sender;
Switch (m_curbtn.name)
{
Case "Button1":
{
A = double. Parse (TextBox1.Text);
b = Double. Parse (TextBox2.Text);
c = a + B;

TextBox3.Text = c+ "";
Break
}
Case "Button2":
{
A = double. Parse (TextBox1.Text);
b = Double. Parse (TextBox2.Text);
c = A * b;

TextBox3.Text = C + "";
Break
}

}

}

private void Button2_Click (object sender, EventArgs e)
{

}

}
}

C # writing a simple calculator

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.