winform窗体计算器(在C#winform里面写计算器,总体思路..)

本文目录
- 在C#winform里面写计算器,总体思路..
- 如何用C#制作一个计算器
- C# 计算器
- 求一个winform计算器代码
- 关于C#中的计算器问题
- C#用winform做计算器
- winform做计算器程序的一个小问题
- C#计算器 (要求:一个textbox接受输入一个计算按钮一个显示结果的textbox)
- winform计算器中textbox输入的最长数值可以输多少
在C#winform里面写计算器,总体思路..
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace jisuan
{
/// 《summary》
/// Form1 的摘要说明。
/// 《/summary》
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
/// 《summary》
/// 必需的设计器变量。
/// 《/summary》
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// 《summary》
/// 清理所有正在使用的资源。
/// 《/summary》
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// 《summary》
/// 设计器支持所需的方法 - 不要使用代码器修改
/// 此方法的内容。
/// 《/summary》
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(24, 72);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 21);
this.textBox1.TabIndex = 0;
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(312, 72);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(100, 21);
this.textBox2.TabIndex = 1;
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(448, 72);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(88, 21);
this.textBox3.TabIndex = 2;
//
// comboBox1
//
this.comboBox1.Items.AddRange(new object {
"+",
"-",
"*",
"/"});
this.comboBox1.Location = new System.Drawing.Point(152, 72);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(121, 20);
this.comboBox1.TabIndex = 3;
this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
//
// button1
//
this.button1.Location = new System.Drawing.Point(64, 184);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(104, 32);
this.button1.TabIndex = 4;
this.button1.Text = "计算";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(216, 192);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 5;
this.button2.Text = "清除";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(376, 192);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 23);
this.button3.TabIndex = 6;
this.button3.Text = "退出";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(656, 366);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.comboBox1);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
/// 《summary》
/// 应用程序的主入口点。
/// 《/summary》
static void Main()
{
Application.Run(new Form1());
}
public double jia(double a,double b)
{
return a+b;
}
public double jian(double a,double b)
{
return a-b;
}
public double cheng(double a,double b)
{
return a*b;
}
public double chu(double a,double b)
{
return a/b;
}
private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
}
private void textBox1_TextChanged(object sender, System.EventArgs e)
{
}
private void button1_Click(object sender, System.EventArgs e)
{
string i=this.comboBox1.SelectedItem.ToString();
switch(i)
{
case "+":this.textBox3.Text=this.jia(double.Parse(this.textBox1.Text),double.Parse(this.textBox2.Text)).ToString();
break;
case "-":this.textBox3.Text=this.jian(double.Parse(this.textBox1.Text),double.Parse(this.textBox2.Text)).ToString();
break;
case "*":this.textBox3.Text=this.cheng(double.Parse(this.textBox1.Text),double.Parse(this.textBox2.Text)).ToString();
break;
case"/" :this.textBox3.Text=this.chu(double.Parse(this.textBox1.Text),double.Parse(this.textBox2.Text)).ToString();
break;
}
}
private void button2_Click(object sender, System.EventArgs e)
{
this.textBox1.Text=null;
this.textBox2.Text=null;
this.textBox3.Text = null;
}
private void button3_Click(object sender, EventArgs e)
{
//this.Hide();
Application.Exit();
//this.Close();
}
}
}
希望可以帮到你
如何用C#制作一个计算器
要新建一个项目,类型是 window窗体应用程序 ,然后使用工具箱中的组件布局计算器窗口,然后双击每个组件,为其添加代码,我写了一个简单计算器代码,我写的这个实现的功能就是整数或小数的四则运算,输错可以清零重新输入。你也不用和我的一模一样,我只是为你提供一个思路,具体情况你自己试着做吧,这个为题不难,我的代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 计算器
{
public partial class Form1 : Form
{
char fh;
double number,temp;
private void Form1_Load(object sender, EventArgs e)
{
}
public Form1()
{
InitializeComponent();
}
private void but1_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "1";
}
private void but2_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "2";
}
private void but3_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "3";
}
private void but4_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "4";
}
private void but5_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "5";
}
private void but6_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "6";
}
private void but7_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "7";
}
private void but8_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "8";
}
private void but9_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "9";
}
private void but0_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "0";
}
private void but10_Click_1(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + ".";
}
private void butAdd_Click(object sender, EventArgs e)
{
number = double.Parse(textBox1.Text);
fh = ’+’;
textBox1.Text = null;
}
private void butMin_Click(object sender, EventArgs e)
{
number = double.Parse(textBox1.Text);
fh = ’-’;
textBox1.Text = null;
}
private void butMul_Click(object sender, EventArgs e)
{
number = double.Parse(textBox1.Text);
fh = ’*’;
textBox1.Text = null;
}
private void butDiv_Click(object sender, EventArgs e)
{
number = double.Parse(textBox1.Text);
fh = ’/’;
textBox1.Text = null;
}
private void butRev_Click(object sender, EventArgs e)
{
fh = ’ ’;
number = 0;
temp = 0;
textBox1.Text = null;
}
private void butApp_Click(object sender, EventArgs e)
{
temp = double.Parse(textBox1.Text);
switch (fh)
{
case ’+’:
number += temp;
break;
case ’-’:
number -= temp;
break;
case ’*’:
number *= temp;
break;
case ’/’:
number /= temp;
break;
}
textBox1.Text = number.ToString();
fh = ’ ’;
number = 0;
temp = 0;
}
}
}
C# 计算器
{
if (txt_show.Text == "0.")
{
if (dot_flag == 1)
{
txt_show.Text = "0.";
}
else
{
txt_show.Text += btn_0.Text;
}
}
else
{
txt_show.Text += btn_0.Text;
}
}
private void btn_1_Click(object sender, EventArgs e)
{
if (txt_show.Text == "0.")
{
if (dot_flag == 1)
{
txt_show.Text = btn_1.Text;
}
else
{
txt_show.Text += btn_1.Text;
}
}
else
{
if (dot_flag == 1)
{
if (op_flag == 1)
{
txt_show.Text += btn_1.Text;
}
else
{
txt_show.Text = btn_1.Text;
op_flag = 1;
}
}
else
{
if (op_flag == 1)
{
txt_show.Text += btn_1.Text;
}
else
{
txt_show.Text = btn_1.Text;
op_flag = 1;
}
}
}
}
private void btn_2_Click(object sender, EventArgs e)
{
if (txt_show.Text == "0.")
{
if (dot_flag == 1)
{
txt_show.Text = btn_2.Text;
}
else
{
txt_show.Text += btn_2.Text;
}
}
else
{
if (dot_flag == 1)
{
if (op_flag == 1)
{
txt_show.Text += btn_2.Text;
}
else
{
txt_show.Text = btn_2.Text;
op_flag = 1;
}
}
else
{
if (op_flag == 1)
{
txt_show.Text += btn_2.Text;
}
else
{
txt_show.Text = btn_2.Text;
op_flag = 1;
}
}
}
}
private void btn_3_Click(object sender, EventArgs e)
{
if (txt_show.Text == "0.")
{
if (dot_flag == 1)
{
txt_show.Text = btn_3.Text;
}
else
{
txt_show.Text += btn_3.Text;
}
}
else
{
if (dot_flag == 1)
{
if (op_flag == 1)
{
txt_show.Text += btn_3.Text;
}
else
{
txt_show.Text = btn_3.Text;
op_flag = 1;
}
}
else
{
if (op_flag == 1)
{
txt_show.Text += btn_3.Text;
}
else
{
txt_show.Text = btn_3.Text;
op_flag = 1;
}
}
}
}
private void btn_4_Click(object sender, EventArgs e)
{
if (txt_show.Text == "0.")
{
if (dot_flag == 1)
{
txt_show.Text = btn_4.Text;
}
else
{
txt_show.Text += btn_4.Text;
}
}
else
{
if (dot_flag == 1)
{
if (op_flag == 1)
{
txt_show.Text += btn_4.Text;
}
else
{
txt_show.Text = btn_4.Text;
op_flag = 1;
}
}
else
{
if (op_flag == 1)
{
txt_show.Text += btn_4.Text;
}
else
{
txt_show.Text = btn_4.Text;
op_flag = 1;
}
}
}
}
private void btn_5_Click(object sender, EventArgs e)
{
if (txt_show.Text == "0.")
{
if (dot_flag == 1)
{
txt_show.Text = btn_5.Text;
}
else
{
txt_show.Text += btn_5.Text;
}
}
else
{
if (dot_flag == 1)
{
if (op_flag == 1)
{
txt_show.Text += btn_5.Text;
}
else
{
txt_show.Text = btn_5.Text;
op_flag = 1;
}
}
else
{
if (op_flag == 1)
{
txt_show.Text += btn_5.Text;
}
else
{
txt_show.Text = btn_5.Text;
op_flag = 1;
}
}
}
}
private void btn_6_Click(object sender, EventArgs e)
{
if (txt_show.Text == "0.")
{
if (dot_flag == 1)
{
txt_show.Text = btn_6.Text;
}
else
{
txt_show.Text += btn_6.Text;
}
}
else
{
if (dot_flag == 1)
{
if (op_flag == 1)
{
txt_show.Text += btn_6.Text;
}
else
{
txt_show.Text = btn_6.Text;
op_flag = 1;
}
}
else
{
if (op_flag == 1)
{
txt_show.Text += btn_6.Text;
}
else
{
txt_show.Text = btn_6.Text;
op_flag = 1;
}
}
}
}
private void btn_7_Click(object sender, EventArgs e)
{
if (txt_show.Text == "0.")
{
if (dot_flag == 1)
{
txt_show.Text = btn_7.Text;
}
else
{
txt_show.Text += btn_7.Text;
}
}
else
{
if (dot_flag == 1)
{
if (op_flag == 1)
{
txt_show.Text += btn_7.Text;
}
else
{
txt_show.Text = btn_7.Text;
op_flag = 1;
}
}
else
{
if (op_flag == 1)
{
txt_show.Text += btn_7.Text;
}
else
{
txt_show.Text = btn_7.Text;
op_flag = 1;
}
}
}
}
private void btn_8_Click(object sender, EventArgs e)
{
if (txt_show.Text == "0.")
{
if (dot_flag == 1)
{
txt_show.Text = btn_8.Text;
}
else
{
txt_show.Text += btn_8.Text;
}
}
else
{
if (dot_flag == 1)
{
if (op_flag == 1)
{
txt_show.Text += btn_8.Text;
}
else
{
txt_show.Text = btn_8.Text;
op_flag = 1;
}
}
else
{
if (op_flag == 1)
{
txt_show.Text += btn_8.Text;
}
else
{
txt_show.Text = btn_8.Text;
op_flag = 1;
}
}
}
}
private void btn_9_Click(object sender, EventArgs e)
{
if (txt_show.Text == "0.")
{
if (dot_flag == 1)
{
txt_show.Text = btn_9.Text;
}
else
{
txt_show.Text += btn_9.Text;
}
}
else
{
if (dot_flag == 1)
{
if (op_flag == 1)
{
txt_show.Text += btn_9.Text;
}
else
{
txt_show.Text = btn_9.Text;
op_flag = 1;
}
}
else
{
if (op_flag == 1)
{
txt_show.Text += btn_9.Text;
}
else
{
txt_show.Text = btn_9.Text;
op_flag = 1;
}
}
}
}
private void btn_dot_Click(object sender, EventArgs e)//处理小数点
{
if (txt_show.Text == "0.")
{
dot_flag = 0;
}
else if (txt_show.Text.Contains("."))
{
dot_flag = 0;
}
else
{
txt_show.Text += btn_dot.Text;
dot_flag = 0;
}
}
private void btn_clear_Click(object sender, EventArgs e)//实现清零
{
dot_flag = 1;
op_flag = 1;
this.txt_show.Text = "0.";
}
private void btn_add_Click(object sender, EventArgs e)//实现加法
{
prevalue = this.txt_show.Text;
i = "+";
op_flag = 0;
}
private void btn_sub_Click(object sender, EventArgs e)
{
prevalue = this.txt_show.Text;
i = "-";
op_flag = 0;
}
private void btn_multi_Click(object sender, EventArgs e)
{
prevalue = this.txt_show.Text;
i = "*";
op_flag = 0;
}
private void btn_div_Click(object sender, EventArgs e)
{
prevalue = this.txt_show.Text;
i = "/";
op_flag = 0;
}
private void btn_equal_Click(object sender, EventArgs e)
{
oldvalue = this.txt_show.Text;
switch (i)
{
case "+":
this.txt_show.Text=System.Convert.ToString(System.Convert.ToDouble(prevalue) +System.Convert.ToDouble(oldvalue));
break;
case "-":
this.txt_show.Text = System.Convert.ToString(System.Convert.ToDouble(prevalue) - System.Convert.ToDouble(oldvalue));
break;
case "*":
this.txt_show.Text = System.Convert.ToString(System.Convert.ToDouble(prevalue) * System.Convert.ToDouble(oldvalue));
break;
case "/":
this.txt_show.Text = System.Convert.ToString(System.Convert.ToDouble(prevalue) / System.Convert.ToDouble(oldvalue));
break;
}
}
}
}
不知道楼主何不合用!!
求一个winform计算器代码
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text += 1;
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text += 2;
}
private void button3_Click(object sender, EventArgs e)
{
textBox1.Text += 3;
}
private void button4_Click(object sender, EventArgs e)
{
textBox1.Text += 4;
}
private void button5_Click(object sender, EventArgs e)
{
textBox1.Text += 5;
}
private void button6_Click(object sender, EventArgs e)
{
textBox1.Text += 6;
}
private void button7_Click(object sender, EventArgs e)
{
textBox1.Text += 7;
}
private void button8_Click(object sender, EventArgs e)
{
textBox1.Text += 8;
}
private void button9_Click(object sender, EventArgs e)
{
textBox1.Text += 9;
}
private void button13_Click(object sender, EventArgs e)
{
textBox1.Text += 0;
}
public double fuhao;
private void jia_Click(object sender, EventArgs e)
{
fuhao = 1;
textBox1.Text += ’+’;
}
private void button11_Click(object sender, EventArgs e)
{
fuhao = 2;
textBox1.Text += ’-’;
}
private void button12_Click(object sender, EventArgs e)
{
fuhao = 3;
textBox1.Text += ’*’;
}
private void button15_Click(object sender, EventArgs e)
{
fuhao = 4;
textBox1.Text += ’/’;
}
private void button14_Click(object sender, EventArgs e)
{
int sum;
char { ’+’, ’-’, ’*’, ’/’ };
string no = this.textBox1.Text.Split(chr);
int a = Convert.ToInt32(no);
int b = Convert.ToInt32(no);
if (fuhao == 1)
{
sum = a + b;
textBox1.Text = Convert.ToString(sum);
}
else if (fuhao == 2)
{
sum = a - b;
textBox1.Text = Convert.ToString(sum);
}
else if (fuhao == 3)
{
sum = a * b;
textBox1.Text = Convert.ToString(sum);
}
else
{
sum = a / b;
textBox1.Text = Convert.ToString(sum);
}
}
关于C#中的计算器问题
这个就是在winform里用的,用的时候在项目的引用中添加Microsoft.JScript名空间,就可以用了。不是只能在web工程用JScript。
反射+JScript的eval函数,代码如下供参考。
using System;
using System.CodeDom.Compiler;
using System.Reflection;
using Microsoft.JScript;
namespace mysoft
{
public class Evaluator
{
public static int EvalToInteger(string statement)
{
string s = EvalToString(statement);
return int.Parse(s.ToString());
}
public static double EvalToDouble(string statement)
{
string s = EvalToString(statement);
return double.Parse(s);
}
public static string EvalToString(string statement)
{
object o = EvalToObject(statement);
return o.ToString();
}
public static object EvalToObject(string statement)
{
return _evaluatorType.InvokeMember(
"Eval",
BindingFlags.InvokeMethod,
null,
_evaluator,
new object { statement }
);
}
static Evaluator()
{
ICodeCompiler compiler;
compiler = new JScriptCodeProvider().CreateCompiler();
CompilerParameters parameters;
parameters = new CompilerParameters();
parameters.GenerateInMemory = true;
CompilerResults results;
results = compiler.CompileAssemblyFromSource(parameters, _jscriptSource);
Assembly assembly = results.CompiledAssembly;
_evaluatorType = assembly.GetType("Evaluator.Evaluator");
_evaluator = Activator.CreateInstance(_evaluatorType);
}
private static object _evaluator = null;
private static Type _evaluatorType = null;
private static readonly string _jscriptSource =
@"package Evaluator
{
class Evaluator
{
public function Eval(expr : String) : String
{
return eval(expr);
}
}
}";
}
}
C#用winform做计算器
混合运算就是核心算法,要写出来也不容易的。
你要知道,普通的计算器操作是这样的。
输入运算值
输入运算符号
输入运算值
获得结果。
如果是这个流程,就不存在什么混合运算的问题,因为事件的发生总是在两个值中。
你明白这个流程不……
能做出混合运算的,看到的一个是google计算器。
如果你不清楚操作流程,代码怎么写也没有意义
winform做计算器程序的一个小问题
appending表示是否添加显示在textbox1上面,没有赋值的话默认为false;
例如你如输入:
200-150=50
在此过程中
依次输入
2 appending为false ,此时textbox1.Text="2"
0 appending为true,此时textbox1.Text="20"
0 appending为true,此时textbox1.Text="200"
- appending为false ,此时textbox1.Text="200"
1 appending为true,此时textbox1.Text=1
5 appending为true,此时textbox1.Text=5
0 appending为true,此时textbox1.Text=0
= appending为false ,此时textbox1.Text=50
C#计算器 (要求:一个textbox接受输入一个计算按钮一个显示结果的textbox)
你参考一下,收集的一个计算器.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace user
{
/// 《summary》
/// Form1 的摘要说明。
/// 《/summary》
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox txtExp;
private System.Windows.Forms.Button btn1;
private System.Windows.Forms.Button btnReverse;
private System.Windows.Forms.Button btnDot;
private System.Windows.Forms.Button btn0;
private System.Windows.Forms.Button btn9;
private System.Windows.Forms.Button btn8;
private System.Windows.Forms.Button btn7;
private System.Windows.Forms.Button btn6;
private System.Windows.Forms.Button btn5;
private System.Windows.Forms.Button btn4;
private System.Windows.Forms.Button btn3;
private System.Windows.Forms.Button btn2;
private System.Windows.Forms.Button btnReset;
private System.Windows.Forms.Button btnAdd;
private System.Windows.Forms.Button btnMinus;
private System.Windows.Forms.Button btnMutiply;
private System.Windows.Forms.Button btnDivide;
private System.Windows.Forms.Button btnBak;
private System.Windows.Forms.Button btnRec;
private System.Windows.Forms.Button btnRemain;
private System.Windows.Forms.Button btnPower;
private System.Windows.Forms.Button btnSqrt;
private System.Windows.Forms.Button btnEqual;
/// 《summary》
/// 必需的设计器变量。
/// 《/summary》
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// 《summary》
/// 清理所有正在使用的资源。
/// 《/summary》
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// 《summary》
/// 设计器支持所需的方法 - 不要使用代码器修改
/// 此方法的内容。
/// 《/summary》
private void InitializeComponent()
{
this.txtExp = new System.Windows.Forms.TextBox();
this.btn1 = new System.Windows.Forms.Button();
this.btnReverse = new System.Windows.Forms.Button();
this.btnDot = new System.Windows.Forms.Button();
this.btn0 = new System.Windows.Forms.Button();
this.btn9 = new System.Windows.Forms.Button();
this.btn8 = new System.Windows.Forms.Button();
this.btn7 = new System.Windows.Forms.Button();
this.btn6 = new System.Windows.Forms.Button();
this.btn5 = new System.Windows.Forms.Button();
this.btn4 = new System.Windows.Forms.Button();
this.btn3 = new System.Windows.Forms.Button();
this.btn2 = new System.Windows.Forms.Button();
this.btnReset = new System.Windows.Forms.Button();
this.btnAdd = new System.Windows.Forms.Button();
this.btnMinus = new System.Windows.Forms.Button();
this.btnRec = new System.Windows.Forms.Button();
this.btnMutiply = new System.Windows.Forms.Button();
this.btnDivide = new System.Windows.Forms.Button();
this.btnRemain = new System.Windows.Forms.Button();
this.btnPower = new System.Windows.Forms.Button();
this.btnSqrt = new System.Windows.Forms.Button();
this.btnEqual = new System.Windows.Forms.Button();
this.btnBak = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// txtExp
//
this.txtExp.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.txtExp.Location = new System.Drawing.Point(8, 8);
this.txtExp.Name = "txtExp";
this.txtExp.Size = new System.Drawing.Size(224, 21);
this.txtExp.TabIndex = 0;
this.txtExp.Text = "";
this.txtExp.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
this.txtExp.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtExp_KeyPress);
//
// btn1
//
this.btn1.Location = new System.Drawing.Point(8, 40);
this.btn1.Name = "btn1";
this.btn1.Size = new System.Drawing.Size(32, 23);
this.btn1.TabIndex = 1;
this.btn1.Text = "1";
this.btn1.Click += new System.EventHandler(this.btnOperand_Click);
//
// btnReverse
//
this.btnReverse.Location = new System.Drawing.Point(72, 112);
this.btnReverse.Name = "btnReverse";
this.btnReverse.Size = new System.Drawing.Size(32, 23);
this.btnReverse.TabIndex = 2;
this.btnReverse.Text = "+/-";
this.btnReverse.Click += new System.EventHandler(this.btnSingleOper_Click);
//
// btnDot
//
this.btnDot.Location = new System.Drawing.Point(40, 112);
this.btnDot.Name = "btnDot";
this.btnDot.Size = new System.Drawing.Size(32, 23);
this.btnDot.TabIndex = 3;
this.btnDot.Text = ".";
this.btnDot.Click += new System.EventHandler(this.btnOperand_Click);
//
// btn0
//
this.btn0.Location = new System.Drawing.Point(8, 112);
this.btn0.Name = "btn0";
this.btn0.Size = new System.Drawing.Size(32, 23);
this.btn0.TabIndex = 4;
this.btn0.Text = "0";
this.btn0.Click += new System.EventHandler(this.btnOperand_Click);
//
// btn9
//
this.btn9.Location = new System.Drawing.Point(72, 88);
this.btn9.Name = "btn9";
this.btn9.Size = new System.Drawing.Size(32, 23);
this.btn9.TabIndex = 5;
this.btn9.Text = "9";
this.btn9.Click += new System.EventHandler(this.btnOperand_Click);
//
// btn8
//
this.btn8.Location = new System.Drawing.Point(40, 88);
this.btn8.Name = "btn8";
this.btn8.Size = new System.Drawing.Size(32, 23);
this.btn8.TabIndex = 6;
this.btn8.Text = "8";
this.btn8.Click += new System.EventHandler(this.btnOperand_Click);
//
// btn7
//
this.btn7.Location = new System.Drawing.Point(8, 88);
this.btn7.Name = "btn7";
this.btn7.Size = new System.Drawing.Size(32, 23);
this.btn7.TabIndex = 7;
this.btn7.Text = "7";
this.btn7.Click += new System.EventHandler(this.btnOperand_Click);
//
// btn6
//
this.btn6.Location = new System.Drawing.Point(72, 64);
this.btn6.Name = "btn6";
this.btn6.Size = new System.Drawing.Size(32, 23);
this.btn6.TabIndex = 8;
this.btn6.Text = "6";
this.btn6.Click += new System.EventHandler(this.btnOperand_Click);
//
// btn5
//
this.btn5.Location = new System.Drawing.Point(40, 64);
this.btn5.Name = "btn5";
this.btn5.Size = new System.Drawing.Size(32, 23);
this.btn5.TabIndex = 9;
this.btn5.Text = "5";
this.btn5.Click += new System.EventHandler(this.btnOperand_Click);
//
// btn4
//
this.btn4.Location = new System.Drawing.Point(8, 64);
this.btn4.Name = "btn4";
this.btn4.Size = new System.Drawing.Size(32, 23);
this.btn4.TabIndex = 10;
this.btn4.Text = "4";
this.btn4.Click += new System.EventHandler(this.btnOperand_Click);
//
// btn3
//
this.btn3.Location = new System.Drawing.Point(72, 40);
this.btn3.Name = "btn3";
this.btn3.Size = new System.Drawing.Size(32, 23);
this.btn3.TabIndex = 11;
this.btn3.Text = "3";
this.btn3.Click += new System.EventHandler(this.btnOperand_Click);
//
// btn2
//
this.btn2.Location = new System.Drawing.Point(40, 40);
this.btn2.Name = "btn2";
this.btn2.Size = new System.Drawing.Size(32, 23);
this.btn2.TabIndex = 12;
this.btn2.Text = "2";
this.btn2.Click += new System.EventHandler(this.btnOperand_Click);
//
// btnReset
//
this.btnReset.Location = new System.Drawing.Point(192, 40);
this.btnReset.Name = "btnReset";
this.btnReset.Size = new System.Drawing.Size(40, 23);
this.btnReset.TabIndex = 23;
this.btnReset.Text = "C";
this.btnReset.Click += new System.EventHandler(this.btnReset_Click);
//
// btnAdd
//
this.btnAdd.Location = new System.Drawing.Point(112, 64);
this.btnAdd.Name = "btnAdd";
this.btnAdd.Size = new System.Drawing.Size(40, 23);
this.btnAdd.TabIndex = 22;
this.btnAdd.Text = "+";
this.btnAdd.Click += new System.EventHandler(this.btnOperator_Click);
//
// btnMinus
//
this.btnMinus.Location = new System.Drawing.Point(152, 64);
this.btnMinus.Name = "btnMinus";
this.btnMinus.Size = new System.Drawing.Size(40, 23);
this.btnMinus.TabIndex = 21;
this.btnMinus.Text = "-";
this.btnMinus.Click += new System.EventHandler(this.btnOperator_Click);
//
// btnRec
//
this.btnRec.Location = new System.Drawing.Point(192, 64);
this.btnRec.Name = "btnRec";
this.btnRec.Size = new System.Drawing.Size(40, 23);
this.btnRec.TabIndex = 20;
this.btnRec.Text = "1/x";
this.btnRec.Click += new System.EventHandler(this.btnSingleOper_Click);
//
// btnMutiply
//
this.btnMutiply.Location = new System.Drawing.Point(112, 88);
this.btnMutiply.Name = "btnMutiply";
this.btnMutiply.Size = new System.Drawing.Size(40, 23);
this.btnMutiply.TabIndex = 19;
this.btnMutiply.Text = "*";
this.btnMutiply.Click += new System.EventHandler(this.btnOperator_Click);
//
// btnDivide
//
this.btnDivide.Location = new System.Drawing.Point(152, 88);
this.btnDivide.Name = "btnDivide";
this.btnDivide.Size = new System.Drawing.Size(40, 23);
this.btnDivide.TabIndex = 18;
this.btnDivide.Text = "/";
this.btnDivide.Click += new System.EventHandler(this.btnOperator_Click);
//
// btnRemain
//
this.btnRemain.Location = new System.Drawing.Point(192, 88);
this.btnRemain.Name = "btnRemain";
this.btnRemain.Size = new System.Drawing.Size(40, 23);
this.btnRemain.TabIndex = 17;
this.btnRemain.Text = "%";
this.btnRemain.Click += new System.EventHandler(this.btnOperator_Click);
//
// btnPower
//
this.btnPower.Location = new System.Drawing.Point(112, 112);
this.btnPower.Name = "btnPower";
this.btnPower.Size = new System.Drawing.Size(40, 23);
this.btnPower.TabIndex = 16;
this.btnPower.Text = "pow";
this.btnPower.Click += new System.EventHandler(this.btnOperator_Click);
//
// btnSqrt
//
this.btnSqrt.Location = new System.Drawing.Point(152, 112);
this.btnSqrt.Name = "btnSqrt";
this.btnSqrt.Size = new System.Drawing.Size(40, 23);
this.btnSqrt.TabIndex = 15;
this.btnSqrt.Text = "sqrt";
this.btnSqrt.Click += new System.EventHandler(this.btnSingleOper_Click);
//
// btnEqual
//
this.btnEqual.Location = new System.Drawing.Point(192, 112);
this.btnEqual.Name = "btnEqual";
this.btnEqual.Size = new System.Drawing.Size(40, 23);
this.btnEqual.TabIndex = 14;
this.btnEqual.Text = "=";
this.btnEqual.Click += new System.EventHandler(this.btnEqual_Click);
//
// btnBak
//
this.btnBak.Location = new System.Drawing.Point(112, 40);
this.btnBak.Name = "btnBak";
this.btnBak.Size = new System.Drawing.Size(80, 23);
this.btnBak.TabIndex = 13;
this.btnBak.Text = "Backspace";
this.btnBak.Click += new System.EventHandler(this.btnBak_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(242, 144);
this.Controls.Add(this.btnReset);
this.Controls.Add(this.btnAdd);
this.Controls.Add(this.btnMinus);
this.Controls.Add(this.btnRec);
this.Controls.Add(this.btnMutiply);
this.Controls.Add(this.btnDivide);
this.Controls.Add(this.btnRemain);
this.Controls.Add(this.btnPower);
this.Controls.Add(this.btnSqrt);
this.Controls.Add(this.btnEqual);
this.Controls.Add(this.btnBak);
this.Controls.Add(this.btn2);
this.Controls.Add(this.btn3);
this.Controls.Add(this.btn4);
this.Controls.Add(this.btn5);
this.Controls.Add(this.btn6);
this.Controls.Add(this.btn7);
this.Controls.Add(this.btn8);
this.Controls.Add(this.btn9);
this.Controls.Add(this.btn0);
this.Controls.Add(this.btnDot);
this.Controls.Add(this.btnReverse);
this.Controls.Add(this.btn1);
this.Controls.Add(this.txtExp);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "计算器";
this.ResumeLayout(false);
}
#endregion
/// 《summary》
/// 应用程序的主入口点。
/// 《/summary》
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
int dotCount = 0;
private void btnOperand_Click(object sender, System.EventArgs e)
{
if(this.ActiveControl==this.btnDot && ++this.dotCount》1)
{
return;
}
this.txtExp.Text += this.ActiveControl.Text;
}
double operand = 0;
string sign = "";
private void btnOperator_Click(object sender, System.EventArgs e)
{
if(this.txtExp.Text!="")
{
this.operand = double.Parse(this.txtExp.Text);
}
this.sign = this.ActiveControl.Text;
this.txtExp.Clear();
}
private void btnEqual_Click(object sender, System.EventArgs e)
{
double result = 0;
switch(this.sign)
{
case "+":
result = this.operand + double.Parse(this.txtExp.Text);
break;
case "-":
result = this.operand - double.Parse(this.txtExp.Text);
break;
case "*":
result = this.operand * double.Parse(this.txtExp.Text);
break;
case "/":
result = this.operand / double.Parse(this.txtExp.Text);
break;
case "%":
result = this.operand % double.Parse(this.txtExp.Text);
break;
case "pow":
result = Math.Pow(this.operand, double.Parse(this.txtExp.Text));
break;
default:
MessageBox.Show("未知运算", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
}
this.txtExp.Text = result.ToString();
}
private void btnSingleOper_Click(object sender, System.EventArgs e)
{
if(this.ActiveControl==this.btnReverse)
{
this.txtExp.Text = (-double.Parse(this.txtExp.Text)).ToString();
}
else if(this.ActiveControl==this.btnRec)
{
this.txtExp.Text = (1/double.Parse(this.txtExp.Text)).ToString();
}
else if(this.ActiveControl==this.btnSqrt)
{
this.txtExp.Text = Convert.ToString(Math.Sqrt(double.Parse(this.txtExp.Text)));
}
}
private void btnBak_Click(object sender, System.EventArgs e)
{
this.txtExp.Text = this.txtExp.Text.Substring(0, this.txtExp.Text.Length-1);
}
private void btnReset_Click(object sender, System.EventArgs e)
{
this.operand = 0;
this.sign = "";
this.dotCount = 0;
this.txtExp.Clear();
}
private void txtExp_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar《’0’ || e.KeyChar》’9’)
{
e.Handled = true;
}
}
}
}
winform计算器中textbox输入的最长数值可以输多少
TextBox.MaxLength 属性
获取或设置文本框中最多允许的字符数。值为 0,表示没有长度限制。
在windows窗体默认为32767,可以自行设置。
尤其像很多登陆密码输入框,有长度限制,例如最长为6为,那么可以将
textbox的MaxLength属性值设为6,即密码可以是0~6位。

更多文章:
blast premier春季赛(csgo战队vitality有谁)
2026年9月7日 21:50
全球新冠肺炎疫情背景下航运发展(盐田港复苏日志:半年历劫从“低谷”到“爆仓” 疫情之后巨轮如何越洋航行)
2026年9月7日 17:10
matlab求解带字母参数方程组(我想matlab求一个关于x,y的方程组 ab c d f e h m n 都是参数)
2026年9月7日 16:30
oracle中的循环语句(下面哪个不是oracle程序设计中的循环语句 a for)
2026年9月7日 15:30
电脑里2个系统怎么删除一个(电脑开机显示有两个系统,如何删除一个)
2026年9月7日 12:20
scrollthrough意思(“scroll”是什么意思)
2026年9月7日 08:00





