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