赞
踩
手把手教你用C#语言写一个简单计算器窗体(附源代码文件和演示视频)!
目录
C#是由C和C++衍生出来的一种安全的、稳定的、简单的、优雅的面向对象编程语言。它在继承C和C++强大功能的同时去掉了一些它们的复杂特性(例如没有宏以及不允许多重继承)。C#综合了VB简单的可视化操作和C++的高运行效率,以其强大的操作能力、优雅的语法风格、创新的语言特性和便捷的面向组件编程的支持成为.NET开发的首选语言。
这里用的是VS2022,可以去官网下载安装(链接如下)Visual Studio: 面向软件开发人员和 Teams 的 IDE 和代码编辑器
Visual Studio2022除了体积较大,可以说是很强大的IDE了可以编写多种语言并且内置编译器,代码补全功能也十分优秀。
首先我们新建一个窗体文件以后,映入眼帘的是一个空白的窗体(窗口),默认为Form1,我们计算器的UI就是要在这上面绘制 这里我们就用到了“控件”,也就是像按钮和文本框这些我们在使用中所需要的东西。
如果vs2022打开后没有显示旁边的工具箱,可以在视图-->工具箱里面打开,或者CTRL+ALT+X;
这里面的分类可以方便我们查找我们所需的工具
我们新建完控件会发现,我们需要设置他的标题,大小还有里面的内容的字体等等
这里就需要用到属性。
我们需要单击控件,选中后,右键,然后点击属性,然后对属性进行修改。
我们双击任何一个按钮(控件)进入代码编辑区
我们会发现,每一个控件包括窗体都对应着一个函数,我们剩下的工作就是编辑代码,补全这些函数,使其联系起来,让代码运行顺利,并且达到我们想要的逻辑效果。
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Runtime.Remoting.Messaging;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
-
- namespace 计算器
- {
- public partial class Form1 : Form
- {
-
- public Form1()
- {
- InitializeComponent();
- }
-
- private void button2_Click(object sender, EventArgs e)
- {
- textBox1.Text += "1";
- }
-
- private void button1_Click(object sender, EventArgs e)
- {
- textBox1.Text += "2";
- }
-
- private void button9_Click(object sender, EventArgs e)
- {
- textBox1.Text += "3";
- }
-
- private void button5_Click(object sender, EventArgs e)
- {
- textBox1.Text += "4";
- }
-
- private void button4_Click(object sender, EventArgs e)
- {
- textBox1.Text += "5";
- }
-
- private void button3_Click(object sender, EventArgs e)
- {
- textBox1.Text += "6";
- }
-
- private void button8_Click(object sender, EventArgs e)
- {
- textBox1.Text += "7";
- }
-
- private void button7_Click(object sender, EventArgs e)
- {
- textBox1.Text += "8";
- }
-
- private void button6_Click(object sender, EventArgs e)
- {
- textBox1.Text += "9";
- }
- private void button11_Click(object sender, EventArgs e)
- {
- textBox1.Text += "0";
- }
- private void button12_Click(object sender, EventArgs e)
- {
- if (textBox1.Text.Contains(".")) return;
- else
- { if (textBox1.Text != "") textBox1.Text += ".";
- }
- }
- private void button10_Click(object sender, EventArgs e)
- {
- textBox1.Text = "";
- textBox2.Text = "";
- chu.Enabled = true;
- cheng.Enabled = true;
- jia.Enabled = true;
- jian.Enabled = true;
- }
-
- private void button13_Click(object sender, EventArgs e)
- {
- if (textBox1.Text != ""&& textBox1.Text != "-") textBox1.Text = Convert.ToString(double.Parse(textBox1.Text) * -1);
- else if (textBox1.Text != "-") textBox1.Text += "-";
- }
-
- private void button14_Click(object sender, EventArgs e)
- {
- if(textBox1.Text!=""&& textBox1.Text != "-") textBox1.Text = Convert.ToString(double.Parse(textBox1.Text) * 0.01);
- }
-
- private void chu_Click(object sender, EventArgs e)
- {
- chu.Enabled = false;
- cheng.Enabled = true;
- jia.Enabled = true;
- jian.Enabled = true;
- if(textBox1.Text!="")
- {
- textBox2.Text = textBox1.Text;
- textBox1.Text = "";
- }
- else textBox2.Text = "0";
-
- }
-
- private void cheng_Click(object sender, EventArgs e)
- {
- chu.Enabled = true;
- cheng.Enabled = false;
- jia.Enabled = true;
- jian.Enabled = true;
- if (textBox1.Text != "")
- {
- textBox2.Text = textBox1.Text;
- textBox1.Text = "";
- }
- else textBox2.Text = "0";
- }
-
- private void jian_Click(object sender, EventArgs e)
- {
- chu.Enabled = true;
- cheng.Enabled = true;
- jia.Enabled = true;
- jian.Enabled = false;
- if (textBox1.Text != "")
- {
- textBox2.Text = textBox1.Text;
- textBox1.Text = "";
- }
- else textBox2.Text = "0";
- }
-
-
- private void jia_Click(object sender, EventArgs e)
- {
- chu.Enabled = true;
- cheng.Enabled = true;
- jia.Enabled = false;
- jian.Enabled = true;
- if (textBox1.Text != "")
- {
- textBox2.Text = textBox1.Text;
- textBox1.Text = "";
- }
- else textBox2.Text = "0";
- }
-
- private void button19_Click(object sender, EventArgs e)
- {
- double hou;
- double qian = double.Parse(textBox2.Text);
- if (textBox1.Text == "") hou = 0;
- else hou = double.Parse(textBox1.Text);
-
- if(chu.Enabled==false)
- {
- if (hou != 0)
- {
- textBox2.Text = "结果如下";
- textBox1.Text = Convert.ToString(qian / hou);
- chu.Enabled = true;
- }
- else
- {
- MessageBox.Show("除数不能为0", "温馨提示");
- textBox1.Text = "";
- return;
- }
-
-
- }
-
- if (cheng.Enabled == false)
- {
- textBox2.Text = "结果如下";
- textBox1.Text = Convert.ToString(qian * hou);
- cheng.Enabled = true;
- }
- if (jia.Enabled == false)
- {
- textBox2.Text = "结果如下";
- textBox1.Text = Convert.ToString(qian + hou);
- jia.Enabled = true;
- }
- if (jian.Enabled == false)
- {
- textBox2.Text = "结果如下";
- textBox1.Text = Convert.ToString(qian - hou);
- jian.Enabled = true;
- }
- if (mod.Enabled == false)
- {
- double s = double.Parse(textBox1.Text);
- int a = (int)s;
- if (a != s)
- {
- MessageBox.Show("小数不能取余数!");
- textBox1.Text = "";
- return;
- }
- else
- {
-
- textBox2.Text = "结果如下";
- textBox1.Text = Convert.ToString(qian % hou);
- mod.Enabled = true;
-
- }
-
- }
-
- }
-
- private void button15_Click(object sender, EventArgs e)
- {
- double r=double.Parse(textBox1.Text);
- if(r<0)
- {
- MessageBox.Show("被开方数不能为0");
- textBox1.Text = "";
- return;
-
- }
- else
- {
- textBox1.Text = Convert.ToString(Math.Sqrt(r));
- }
-
- }
-
-
- private void button16_Click(object sender, EventArgs e)
- {
-
- double s = double.Parse(textBox1.Text);
- int a=(int)s;
- if(a!=s)
- {
- MessageBox.Show("小数不能取余数!");
- textBox1.Text = "";
- return;
- }
- else
- {
- mod.Enabled = false;
- chu.Enabled = true;
- cheng.Enabled = true;
- jia.Enabled = true;
- jian.Enabled = true;
- if (textBox1.Text != "")
- {
- textBox2.Text = textBox1.Text;
- textBox1.Text = "";
- }
- else textBox2.Text = "0";
- }
-
-
- }
-
- private void button17_Click(object sender, EventArgs e)
- {
- if (textBox1.Text != "") textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
- else return;
-
- }
-
- private void button16_Click_1(object sender, EventArgs e)
- {
-
- Form2 B2 = new Form2();
- B2.Show();
-
- }
-
- private void button18_Click(object sender, EventArgs e)
- {
- double r = double.Parse(textBox1.Text);
-
- textBox2.Text = "结果如下";
- textBox1.Text = Convert.ToString(r*r);
-
- }
-
- private void button20_Click(object sender, EventArgs e)
- {
- double s = double.Parse(textBox1.Text);
- int a = (int)s;
-
- if (a != s)
- {
- MessageBox.Show("小数不能求阶乘");
- textBox1.Text = "";
- return;
- }
- else
- {
- if(a==0)
- {
- textBox1.Text = "1";
- return;
- }
- int cnt = 1;
- for(int i = 1; i <= a; i++)
- {
- cnt *= i;
- }
- textBox2.Text = "结果如下";
- textBox1.Text = Convert.ToString(cnt);
- }
- }
- }
- }
本计算器还有另一个“彩蛋”功能(如下图)
这里我们要学的东西分为两个:
首先,在视图中打开解决方案资源管理器
然后选中项目,右键点击鼠标 ,然后在添加中找到窗体。
首先,插入所需控件PictureBox
单击选中后,右键选择图像
然后选择本地文件后插入
然后修改图片属性,修改其尺寸填充方式
然后我们仿照Form1的方式设计UI并且编写和填充函数。
- 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 计算器
- {
- public partial class Form2 : Form
- {
- public Form2()
- {
- InitializeComponent();
- }
-
- private void button16_Click(object sender, EventArgs e)
- {
- if(textBox1.Text=="")
- {
- MessageBox.Show("请输入年收入!");
- return;
-
- }
- else
- {
- double r=double.Parse(textBox1.Text);
- double cnt = 0;
- if (r <= 36000) cnt = r * 0.03;
- else if (r <= 144000) cnt = 36000 * 0.03 + (r-36000) * 0.1;
- else if (r <= 300000) cnt = 36000 * 0.03 + (144000 - 36000) * 0.1+ (r- 144000)*0.2;
- else if (r <= 420000) cnt = 36000 * 0.03 + (144000 - 36000) * 0.1 + (300000- 144000) * 0.2+(r-30000)*0.25;
- else if (r <= 660000) cnt = 36000 * 0.03 + (144000 - 36000) * 0.1 + (300000 - 144000) * 0.2 + (420000 - 30000) * 0.25+(r-420000)*0.3;
- else if (r <= 960000) cnt = 36000 * 0.03 + (144000 - 36000) * 0.1 + (300000 - 144000) * 0.2 + (420000 - 30000) * 0.25 + (660000 - 420000) * 0.3+(r-660000)*0.35;
- else if (r > 960000) cnt = 36000 * 0.03 + (144000 - 36000) * 0.1 + (300000 - 144000) * 0.2 + (420000 - 30000) * 0.25 + (660000 - 420000) * 0.3 + (960000 - 660000) * 0.35+(r-960000)*0.45;
-
- textBox2.Text=Convert.ToString(cnt);
-
-
- }
-
- }
-
- private void Form2_Load(object sender, EventArgs e)
- {
-
- }
- }
- }
C#简单计算器效果演示
点击我链接直达,记得点赞支持一下哦!https://download.csdn.net/download/xiaozhang0316/87403972
(新人小白发帖 大佬不喜勿喷!)
有问题欢迎随时联系我!!!
感谢支持!!!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。