winform 计算器控件ui
时间: 2023-09-11 21:07:23 浏览: 191
winform控件
3星 · 编辑精心推荐
WinForm的计算器控件UI通常会包含数字按钮、运算符按钮、清除按钮和等号按钮。以下是一个简单的示例:
1. 首先,添加一个文本框控件用于显示计算结果:
```c#
private System.Windows.Forms.TextBox txtResult;
```
2. 接下来,添加数字按钮和运算符按钮。这里我们使用Button控件,并设置其Text属性为相应的数字或运算符:
```c#
private System.Windows.Forms.Button btn0;
private System.Windows.Forms.Button btn1;
private System.Windows.Forms.Button btn2;
private System.Windows.Forms.Button btn3;
private System.Windows.Forms.Button btn4;
private System.Windows.Forms.Button btn5;
private System.Windows.Forms.Button btn6;
private System.Windows.Forms.Button btn7;
private System.Windows.Forms.Button btn8;
private System.Windows.Forms.Button btn9;
private System.Windows.Forms.Button btnAdd;
private System.Windows.Forms.Button btnSubtract;
private System.Windows.Forms.Button btnMultiply;
private System.Windows.Forms.Button btnDivide;
```
3. 添加清除按钮和等号按钮:
```c#
private System.Windows.Forms.Button btnClear;
private System.Windows.Forms.Button btnEqual;
```
4. 将这些控件添加到窗体上,并设置它们的位置和大小:
```c#
this.txtResult.Location = new System.Drawing.Point(12, 12);
this.txtResult.Size = new System.Drawing.Size(260, 50);
this.btnClear.Location = new System.Drawing.Point(12, 78);
this.btnClear.Size = new System.Drawing.Size(50, 50);
this.btnClear.Text = "C";
this.btnDivide.Location = new System.Drawing.Point(196, 78);
this.btnDivide.Size = new System.Drawing.Size(50, 50);
this.btnDivide.Text = "/";
// 其他按钮的位置和大小类似
```
5. 最后,添加事件处理程序来实现这个计算器的功能。例如,当用户按下数字按钮时,我们需要将该数字添加到文本框中:
```c#
private void btn1_Click(object sender, EventArgs e)
{
this.txtResult.Text += "1";
}
```
其他按钮的事件处理程序的实现方式类似,可以根据需要进行修改。完整的示例代码如下:
```c#
private System.Windows.Forms.TextBox txtResult;
private System.Windows.Forms.Button btn0;
private System.Windows.Forms.Button btn1;
private System.Windows.Forms.Button btn2;
private System.Windows.Forms.Button btn3;
private System.Windows.Forms.Button btn4;
private System.Windows.Forms.Button btn5;
private System.Windows.Forms.Button btn6;
private System.Windows.Forms.Button btn7;
private System.Windows.Forms.Button btn8;
private System.Windows.Forms.Button btn9;
private System.Windows.Forms.Button btnAdd;
private System.Windows.Forms.Button btnSubtract;
private System.Windows.Forms.Button btnMultiply;
private System.Windows.Forms.Button btnDivide;
private System.Windows.Forms.Button btnClear;
private System.Windows.Forms.Button btnEqual;
public Form1()
{
InitializeComponent();
this.txtResult = new System.Windows.Forms.TextBox();
this.btn0 = new System.Windows.Forms.Button();
this.btn1 = new System.Windows.Forms.Button();
this.btn2 = new System.Windows.Forms.Button();
this.btn3 = new System.Windows.Forms.Button();
this.btn4 = new System.Windows.Forms.Button();
this.btn5 = new System.Windows.Forms.Button();
this.btn6 = new System.Windows.Forms.Button();
this.btn7 = new System.Windows.Forms.Button();
this.btn8 = new System.Windows.Forms.Button();
this.btn9 = new System.Windows.Forms.Button();
this.btnAdd = new System.Windows.Forms.Button();
this.btnSubtract = new System.Windows.Forms.Button();
this.btnMultiply = new System.Windows.Forms.Button();
this.btnDivide = new System.Windows.Forms.Button();
this.btnClear = new System.Windows.Forms.Button();
this.btnEqual = new System.Windows.Forms.Button();
this.txtResult.Location = new System.Drawing.Point(12, 12);
this.txtResult.Size = new System.Drawing.Size(260, 50);
this.btnClear.Location = new System.Drawing.Point(12, 78);
this.btnClear.Size = new System.Drawing.Size(50, 50);
this.btnClear.Text = "C";
this.btnDivide.Location = new System.Drawing.Point(196, 78);
this.btnDivide.Size = new System.Drawing.Size(50, 50);
this.btnDivide.Text = "/";
// 其他按钮的位置和大小类似
this.btn1.Click += new System.EventHandler(this.btn1_Click);
this.btn2.Click += new System.EventHandler(this.btn2_Click);
this.btn3.Click += new System.EventHandler(this.btn3_Click);
this.btn4.Click += new System.EventHandler(this.btn4_Click);
this.btn5.Click += new System.EventHandler(this.btn5_Click);
this.btn6.Click += new System.EventHandler(this.btn6_Click);
this.btn7.Click += new System.EventHandler(this.btn7_Click);
this.btn8.Click += new System.EventHandler(this.btn8_Click);
this.btn9.Click += new System.EventHandler(this.btn9_Click);
this.btn0.Click += new System.EventHandler(this.btn0_Click);
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
this.btnSubtract.Click += new System.EventHandler(this.btnSubtract_Click);
this.btnMultiply.Click += new System.EventHandler(this.btnMultiply_Click);
this.btnDivide.Click += new System.EventHandler(this.btnDivide_Click);
this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
this.btnEqual.Click += new System.EventHandler(this.btnEqual_Click);
this.Controls.Add(this.txtResult);
this.Controls.Add(this.btn0);
this.Controls.Add(this.btn1);
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.btnAdd);
this.Controls.Add(this.btnSubtract);
this.Controls.Add(this.btnMultiply);
this.Controls.Add(this.btnDivide);
this.Controls.Add(this.btnClear);
this.Controls.Add(this.btnEqual);
}
private void btn1_Click(object sender, EventArgs e)
{
this.txtResult.Text += "1";
}
private void btn2_Click(object sender, EventArgs e)
{
this.txtResult.Text += "2";
}
private void btn3_Click(object sender, EventArgs e)
{
this.txtResult.Text += "3";
}
private void btn4_Click(object sender, EventArgs e)
{
this.txtResult.Text += "4";
}
private void btn5_Click(object sender, EventArgs e)
{
this.txtResult.Text += "5";
}
private void btn6_Click(object sender, EventArgs e)
{
this.txtResult.Text += "6";
}
private void btn7_Click(object sender, EventArgs e)
{
this.txtResult.Text += "7";
}
private void btn8_Click(object sender, EventArgs e)
{
this.txtResult.Text += "8";
}
private void btn9_Click(object sender, EventArgs e)
{
this.txtResult.Text += "9";
}
private void btn0_Click(object sender, EventArgs e)
{
this.txtResult.Text += "0";
}
private void btnAdd_Click(object sender, EventArgs e)
{
this.txtResult.Text += "+";
}
private void btnSubtract_Click(object sender, EventArgs e)
{
this.txtResult.Text += "-";
}
private void btnMultiply_Click(object sender, EventArgs e)
{
this.txtResult.Text += "*";
}
private void btnDivide_Click(object sender, EventArgs e)
{
this.txtResult.Text += "/";
}
private void btnClear_Click(object sender, EventArgs e)
{
this.txtResult.Text = "";
}
private void btnEqual_Click(object sender, EventArgs e)
{
try
{
DataTable dt = new DataTable();
var v = dt.Compute(this.txtResult.Text, "");
this.txtResult.Text = v.ToString();
}
catch(Exception ex)
{
this.txtResult.Text = "Error";
}
}
```
阅读全文