一、界面设置。
1、设置两个Label标签,并将其AutoSize属性设置为False,此时可以放大Label标签。
2、具体界面如下所示。
二、代码编写。
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 MyFirstDemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public double num1 = 0,num2 = 0,sum = 0;
public char ch = '\0';
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (this.label2.Text == "")
{
this.label1.Text += "1";
}
else {
this.label1.Text = this.label2.Text;
this.label2.Text = "";
}
}
private void button2_Click(object sender, EventArgs e)
{
if (this.label2.Text == "")
{
this.label1.Text += "2";
}
else
{
this.label1.Text = this.label2.Text;
this.label2.Text = "";
}
}
private void button3_Click(object sender, EventArgs e)
{
if (this.label2.Text == "")
{
this.label1.Text += "3";
}
else
{
this.label1.Text = this.label2.Text;
this.label2.Text = "";
}
}
private void button4_Click(object sender, EventArgs e)
{
if (this.label2.Text == "")
{
this.label1.Text += "4";
}
else
{
this.label1.Text = this.label2.Text;
this.label2.Text = "";
}
}
private void button5_Click(object sender, EventArgs e)
{
if (this.label2.Text == "")
{
this.label1.Text += "5";
}
else
{
this.label1.Text = this.label2.Text;
this.label2.Text = "";
}
}
private void button6_Click(object sender, EventArgs e)
{
if (this.label2.Text == "")
{
this.label1.Text += "6";
}
else
{
this.label1.Text = this.label2.Text;
this.label2.Text = "";
}
}
private void button7_Click(object sender, EventArgs e)
{
if (this.label2.Text == "")
{
this.label1.Text += "7";
}
else
{
this.label1.Text = this.label2.Text;
this.label2.Text = "";
}
}
private void button8_Click(object sender, EventArgs e)
{
if (this.label2.Text == "")
{
this.label1.Text += "8";
}
else
{
this.label1.Text = this.label2.Text;
this.label2.Text = "";
}
}
private void button9_Click(object sender, EventArgs e)
{
if (this.label2.Text == "")
{
this.label1.Text += "9";
}
else
{
this.label1.Text = this.label2.Text;
this.label2.Text = "";
}
}
private void button10_Click(object sender, EventArgs e)
{
if (this.label2.Text == "")
{
this.label1.Text += "0";
}
else
{
this.label1.Text = this.label2.Text;
this.label2.Text = "";
}
}
private void button11_Click(object sender, EventArgs e)
{
if (this.label2.Text == "")
{
this.label1.Text += ".";
}
else
{
this.label1.Text = this.label2.Text;
this.label2.Text = "";
}
}
private void button12_Click(object sender, EventArgs e)
{
this.label1.Text = "";
this.label2.Text = "";
}
private void button16_Click(object sender, EventArgs e)
{
if (this.label2.Text == "")
{
this.label1.Text += "/";
}
else
{
this.label1.Text = this.label2.Text+"/";
this.label2.Text = "";
}
ch = '/';
}
private void button15_Click(object sender, EventArgs e)
{
if (this.label2.Text == "")
{
this.label1.Text += "*";
}
else
{
this.label1.Text = this.label2.Text+"*";
this.label2.Text = "";
}
ch = '*';
}
private void button14_Click(object sender, EventArgs e)
{
if (this.label2.Text == "")
{
this.label1.Text += "-";
}
else
{
this.label1.Text = this.label2.Text+"-";
this.label2.Text = "";
}
ch = '-';
}
private void button13_Click(object sender, EventArgs e)
{
if (this.label2.Text == "")
{
this.label1.Text += "+";
}
else
{
this.label1.Text = this.label2.Text+"+";
this.label2.Text = "";
}
ch = '+';
}
private void button17_Click(object sender, EventArgs e)
{
switch (ch) {
case ('+'):
num1 = Convert.ToDouble(this.label1.Text.Split('+').GetValue(0));
num2 = Convert.ToDouble(this.label1.Text.Split('+').GetValue(1));
sum = num1 + num2;
break;
case ('-'):
num1 = Convert.ToDouble(this.label1.Text.Split('-').GetValue(0));
num2 = Convert.ToDouble(this.label1.Text.Split('-').GetValue(1));
sum = num1 - num2;
break;
case ('*'):
num1 = Convert.ToDouble(this.label1.Text.Split('*').GetValue(0));
num2 = Convert.ToDouble(this.label1.Text.Split('*').GetValue(1));
sum = num1 * num2;
break;
case ('/'):
num1 = Convert.ToDouble(this.label1.Text.Split('/').GetValue(0));
num2 = Convert.ToDouble(this.label1.Text.Split('/').GetValue(1));
if (num2 == 0) {
MessageBox.Show("除数不能为0!");
throw new Exception("除数不能为0!");
}
sum = num1 / num2;
break;
}
this.label1.Text += "=";
this.label2.Text = Convert.ToString(sum);
}
}
}
三、结果展示。
存在问题:不支持多个运算符一起操作。
-----END-----