使用C#windows窗体应用程序做一个简易计算器

纯属闲的没事,写一个普通计算器玩玩

第一步是自己在Form1设计.cs里面随便设计(喜欢啥样整啥样,这里设计属性里面的基本操作就不介绍了)

第二步就是直接写代码了(这里我是将 

this.button2.Visible = false;

this.richTextBox1.Visible = false;

两个工具设置为了隐藏(其实不改也可以),如果像我这么做的话就要在主程序的结构里加上

        //将“确定”选项隐藏
        private void button2_Click(object sender, EventArgs e)
        {
            this.richTextBox1.Hide();
            this.button2.Hide();
        }

这样一段话)

首先定义一种法则,然后逐一复制即可(有手就行:最简单的加减乘除了)

如加法:

 if (textBox2.Text == "+")
            {
                this.richTextBox1.Visible = true;
                this.button2.Visible = true;
                richTextBox1.Text = (double.Parse(textBox1.Text) + double.Parse(textBox3.Text)).ToString();

那么整段代码就是:(form.cs里的)

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 Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }
        //将“确定”选项隐藏
        private void button2_Click(object sender, EventArgs e)
        {
            this.richTextBox1.Hide();
            this.button2.Hide();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox2.Text == "+")
            {
                this.richTextBox1.Visible = true;
                this.button2.Visible = true;
                richTextBox1.Text = (double.Parse(textBox1.Text) + double.Parse(textBox3.Text)).ToString();
            }

            if (textBox2.Text == "-")
            {
                this.richTextBox1.Visible = true;
                this.button2.Visible = true;
                richTextBox1.Text = (double.Parse(textBox1.Text) - double.Parse(textBox3.Text)).ToString();
            }

            if (textBox2.Text == "*")
            {
                this.richTextBox1.Visible = true;
                this.button2.Visible = true;
                richTextBox1.Text = (double.Parse(textBox1.Text) * double.Parse(textBox3.Text)).ToString();
            }

            if (textBox2.Text == "/")
            {
                this.richTextBox1.Visible = true;
                this.button2.Visible = true;
                richTextBox1.Text = (double.Parse(textBox1.Text) / double.Parse(textBox3.Text)).ToString();
            }
        }

        private void 说明ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("适用于计算简易加减乘除,运算符号为:加+,减-,乘*,除/");
        }
    }
}

(form.designers.cs里的则是)直接复制就行,其实就是上述的那个:将true改为了flase

namespace 简易计算器
{
    partial class Form1
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要修改
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.button1 = new System.Windows.Forms.Button();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.textBox3 = new System.Windows.Forms.TextBox();
            this.richTextBox1 = new System.Windows.Forms.RichTextBox();
            this.button2 = new System.Windows.Forms.Button();
            this.label4 = new System.Windows.Forms.Label();
            this.menuStrip1 = new System.Windows.Forms.MenuStrip();
            this.说明ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.menuStrip1.SuspendLayout();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(190, 149);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(45, 15);
            this.label1.TabIndex = 0;
            this.label1.Text = "数字1";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(365, 149);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(52, 15);
            this.label2.TabIndex = 1;
            this.label2.Text = "运算符";
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(541, 149);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(45, 15);
            this.label3.TabIndex = 2;
            this.label3.Text = "数字2";
            // 
            // button1
            // 
            this.button1.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.button1.Location = new System.Drawing.Point(169, 317);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(94, 48);
            this.button1.TabIndex = 3;
            this.button1.Text = "计算\r\n";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(166, 204);
            this.textBox1.Multiline = true;
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(105, 36);
            this.textBox1.TabIndex = 4;
            // 
            // textBox2
            // 
            this.textBox2.Location = new System.Drawing.Point(341, 204);
            this.textBox2.Multiline = true;
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new System.Drawing.Size(111, 36);
            this.textBox2.TabIndex = 5;
            this.textBox2.TextChanged += new System.EventHandler(this.textBox2_TextChanged);
            // 
            // textBox3
            // 
            this.textBox3.Location = new System.Drawing.Point(519, 204);
            this.textBox3.Multiline = true;
            this.textBox3.Name = "textBox3";
            this.textBox3.Size = new System.Drawing.Size(106, 36);
            this.textBox3.TabIndex = 6;
            // 
            // richTextBox1
            // 
            this.richTextBox1.Location = new System.Drawing.Point(324, 317);
            this.richTextBox1.Name = "richTextBox1";
            this.richTextBox1.Size = new System.Drawing.Size(195, 121);
            this.richTextBox1.TabIndex = 7;
            this.richTextBox1.Text = "";
            this.richTextBox1.Visible = false;
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(324, 467);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(142, 40);
            this.button2.TabIndex = 8;
            this.button2.Text = "确定";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.UseWaitCursor = true;
            this.button2.Visible = false;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Font = new System.Drawing.Font("华文楷体", 24F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.label4.Location = new System.Drawing.Point(299, 62);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(220, 44);
            this.label4.TabIndex = 9;
            this.label4.Text = "简易计算器";
            // 
            // menuStrip1
            // 
            this.menuStrip1.ImageScalingSize = new System.Drawing.Size(20, 20);
            this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.说明ToolStripMenuItem});
            this.menuStrip1.Location = new System.Drawing.Point(0, 0);
            this.menuStrip1.Name = "menuStrip1";
            this.menuStrip1.Size = new System.Drawing.Size(856, 28);
            this.menuStrip1.TabIndex = 10;
            this.menuStrip1.Text = "menuStrip1";
            // 
            // 说明ToolStripMenuItem
            // 
            this.说明ToolStripMenuItem.Name = "说明ToolStripMenuItem";
            this.说明ToolStripMenuItem.Size = new System.Drawing.Size(51, 24);
            this.说明ToolStripMenuItem.Text = "说明";
            this.说明ToolStripMenuItem.Click += new System.EventHandler(this.说明ToolStripMenuItem_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(856, 524);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.richTextBox1);
            this.Controls.Add(this.textBox3);
            this.Controls.Add(this.textBox2);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.menuStrip1);
            this.MainMenuStrip = this.menuStrip1;
            this.Name = "Form1";
            this.Text = "Form1";
            this.menuStrip1.ResumeLayout(false);
            this.menuStrip1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.TextBox textBox2;
        private System.Windows.Forms.TextBox textBox3;
        private System.Windows.Forms.RichTextBox richTextBox1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.MenuStrip menuStrip1;
        private System.Windows.Forms.ToolStripMenuItem 说明ToolStripMenuItem;
    }
}

但是要注意!!! richTextBox1.Text = (double.Parse(textBox1.Text) +double.Parse(textBox3.Text)).ToString();在这段话中想运算小数需要用到double,不能用int!int只能作为整数运算,而double是双浮点精度,精度较高。

下面为运行结果:

 本程序由b站大佬:Lothric洛斯里克  教学,希望能帮助到各位入门级C#同学!(大佬请划走)(如有侵权请告知!!!)  

  • 8
    点赞
  • 64
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Window Form 应用程序是基于 Windows 操作系统 GUI 界面的应用程序,它使用了多线程技术。在 Window Form 应用程序中,主线程通常用于处理用户界面(UI)操作,而其他线程则用于执行后台任务,例如进行网络通信、文件处理等。这种方式可以避免长时间的操作阻塞 UI 线程,让应用程序更加流畅。 在 Window Form 应用程序中,创建线程的方式与标准的 C# 程序一样,可以使用 Thread 类或 ThreadPool 类来创建线程。但需要注意的是,在 UI 线程中访问 UI 控件是不安全的,必须使用 Invoke 或 BeginInvoke 方法来让 UI 线程更新 UI 控件。 例如,以下代码演示了如何在 Window Form 应用程序中创建一个后台线程来执行耗时的任务: ``` private void btnStart_Click(object sender, EventArgs e) { // 创建后台线程 Thread thread = new Thread(new ThreadStart(DoWork)); thread.Start(); } private void DoWork() { // 执行耗时的任务 // ... // 更新 UI 控件(需要使用 Invoke 或 BeginInvoke 方法) this.Invoke(new Action(() => { // 更新 UI 控件 // ... })); } ``` 在上述代码中,btnStart_Click 方法是 UI 线程中的事件处理程序,当用户点击按钮时会创建一个后台线程来执行任务。DoWork 方法是后台线程的入口点,它执行耗时的任务,并使用 Invoke 方法来更新 UI 控件。这样可以保证 UI 线程不会被阻塞,同时也可以让用户获得更好的体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值