计算器需求分析
一、界面设计
1.做一个显示屏
2.17个按钮(0-9,±×÷%=,CE)
二、需要实现的功能
1.输入第一个数字
2.选择运算类型
3.输入第二个数字
4.按下等号计算出结果,结果显示在显示屏上
三、实现步骤
1.先做界面
a.显示屏 textbox、listbox、label
b.使用17个button,button上的文本改成对应的数字符号
2.
补充:申请两个int类型变量,第一个num1装第一个数字
第二个num2装第二个数字
(1).输入第一个数字,当点一个数字按钮,屏幕上显示一个,之前显示的数字在前面呆着
a1.添加按钮的cilck事件
a2.事件触发,将按钮代表的数字显示textbox1的text
(2).当输入符号的时候,清除屏幕,但是后台必须记录好第一个数字
b1.添加符号按钮的click事件
b2.当点任何一个符号按钮用一个变量num1装刚才输入的textbox1中的数字
(3).输入第二个数字
c1. 当点任何一个符号按钮用一个变量num2装刚才输入的textbox1中的数字
(4).按下等号按钮,显示屏上面的文本改变成两个数字的运算结果
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 _01计算机精简版
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//1
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "1";//点击之后可以继续点击,+在这里是拼接的作用
}
//2
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "2";
}
//3
private void button3_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "3";
}
//4
private void button4_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "4";
}
//5
private void button5_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "5";
}
//6
private void button6_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "6";
}
//7
private void button7_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "7";
}
//8
private void button8_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "8";
}
//9
private void button9_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "9";
}
//0
private void button10_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "0";
}
//+
int num1;//装第一个数字的变量
int num2;//装第二个数字的变量
string fun = "";//定义一个空的字符串用来记录点击的运算类型
private void button11_Click(object sender, EventArgs e)
{
jisuan();
fun = "jia";
}
//方法
private void jisuan()
{
//获取运算的第一个数字(前一个数字);将字符串类型转换为int类型(int.parse())
// num1 = Convert.ToInt32(textBox1.Text);第二种转换方式convert
num1 = int.Parse(textBox1.Text);
textBox1.Text = "";//清除屏幕
}
//-
private void button12_Click(object sender, EventArgs e)
{
jisuan();
fun = "jian";
}
//*
private void button13_Click(object sender, EventArgs e)
{
jisuan();
fun = "cheng";
}
//÷
private void button14_Click(object sender, EventArgs e)
{
jisuan();
fun = "chu";
}
//%
private void button15_Click(object sender, EventArgs e)
{
jisuan();
fun = "%";
}
//=
private void button16_Click(object sender, EventArgs e)
{
num2 = int.Parse(textBox1.Text);//记录第二个数字
textBox1.Text = "";
if (fun=="jia")//判断计算类型
{
textBox1.Text = (num1 + num2).ToString();//括号里进行计算,计算的结果转化为string类型,并显示在屏幕(textbox1)里;
}
if (fun=="jian")
{
textBox1.Text = (num1 - num2).ToString();
}
if (fun=="cheng")
{
textBox1.Text = (num1 * num2).ToString();
}
if (fun=="chu")
{
textBox1.Text = (num1 / num2).ToString();
}
if (fun=="%")
{
textBox1.Text = (num1 % num2).ToString();
}
}
//CE
private void button17_Click(object sender, EventArgs e)
{
textBox1.Text = "";
fun = "";
}
}
}
注:textbox1设置属性multiline值为true可以跨行;
Readonly值为true只读;
tabstop值为false失去焦点(光标),此时光标给了button1.或在设置一个textbox2用textbox1覆盖,光标给textbox2.
tabindex占用tab键的顺序索引