纯属闲的没事,写一个普通计算器玩玩
第一步是自己在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#同学!(大佬请划走)(如有侵权请告知!!!)