我们在生活中,或多或少都使用过计算器。那么我们是否可以自己制作一款计算器呢,答案当然是可以的。
功能介绍
我们需要将0~9这10个数字的按键,还需要四则运算需要的加、减、乘、除等,具体界面如下。

步骤
1. 打开VS,创建Windows窗体应用

2. 选择项目文件夹
根据自己的实际存储位置,进行更改。


3. 打开 视图 中的工具箱,通过拖拉相关配件,进行计算器页面的设计,注意相关按钮的名字需要自己编辑。

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 WindowsFormsApplication2
- {
- public partial class Form1 : Form
- {
- double a = 0;
- double b = 0;
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
-
- b = double.Parse(textBox1.Text);
- textBox1.Text += "+";
- }
- private void button6_Click(object sender, EventArgs e)
- {
- textBox1.Text += "1";
- }
- private void button7_Click(object sender, EventArgs e)
- {
- textBox1.Text += "2";
- }
- private void button8_Click(object sender, EventArgs e)
- {
- textBox1.Text += ".";
- }
- private void button5_Click(object sender, EventArgs e)
- {
- DataTable dt = new DataTable();
- var res = dt.Compute(textBox1.Text, "");
- textBox1.Text = Convert.ToString(res);
- }
- private void button2_Click(object sender, EventArgs e)
- {
- b = double.Parse(textBox1.Text);
- textBox1.Text += "-";
- }
- private void button3_Click(object sender, EventArgs e)
- {
- b = double.Parse(textBox1.Text);
- textBox1.Text += "*";
- }
- private void button4_Click(object sender, EventArgs e)
- {
- b = double.Parse(textBox1.Text);
- textBox1.Text += "/";
- }
- private void button10_Click(object sender, EventArgs e)
- {
- textBox1.Text += "3";
- }
- private void button9_Click(object sender, EventArgs e)
- {
- textBox1.Text += "4";
- }
- private void button11_Click(object sender, EventArgs e)
- {
- textBox1.Text += "5";
- }
- private void button12_Click(object sender, EventArgs e)
- {
- textBox1.Text += "6";
- }
- private void button13_Click(object sender, EventArgs e)
- {
- textBox1.Text += "7";
- }
- private void button14_Click(object sender, EventArgs e)
- {
- textBox1.Text += "8";
- }
- private void button15_Click(object sender, EventArgs e)
- {
- textBox1.Text += "9";
- }
- private void button16_Click(object sender, EventArgs e)
- {
- textBox1.Text += "0";
- }
- }
- }
