C#计算器设计
1.掌握计算器界面设计的方法。
2.掌握和分析需求,并根据需求实现相应的功能 。
3.熟悉系统调试等相关能力,形成初步的有实用意义的计算器程序。
计算器运行结果如下:
public static List<char> inputStr = new List<char>(1000); //用户的输入
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btn_0_Click(object sender, EventArgs e)
{
inputStr.Add('0');
richTextBox1.AppendText("0");
}
private void btn_Dot_Click(object sender, EventArgs e)
{
inputStr.Add('.');
richTextBox1.AppendText(".");
}
private void btn_1_Click(object sender, EventArgs e)
{
inputStr.Add('1');
richTextBox1.AppendText("1");
}
private void btn_2_Click(object sender, EventArgs e)
{
inputStr.Add('2');
richTextBox1.AppendText("2");
}
private void btn_3_Click(object sender, EventArgs e)
{
inputStr.Add('3');
richTextBox1.AppendText("3");
}
private void btn_4_Click(object sender, EventArgs e)
{
inputStr.Add('4');
richTextBox1.AppendText("4");
}
private void btn_5_Click(object sender, EventArgs e)
{
inputStr.Add('5');
richTextBox1.AppendText("5");
}
private void btn_6_Click(object sender, EventArgs e)
{
inputStr.Add('6');
richTextBox1.AppendText("6");
}
private void btn_7_Click(object sender, EventArgs e)
{
inputStr.Add('7');
richTextBox1.AppendText("7");
}
private void btn_8_Click(object sender, EventArgs e)
{
inputStr.Add('8');
richTextBox1.AppendText("8");
}
private void btn_9_Click(object sender, EventArgs e)
{
inputStr.Add('9');
richTextBox1.AppendText("9");
}
private void btn_Left_Click(object sender, EventArgs e)
{
inputStr.Add('(');
richTextBox1.AppendText("(");
}
private void btn_Rig_Click(object sender, EventArgs e)
{
inputStr.Add(')');
richTextBox1.AppendText(")");
}
private void btn_Equal_Click(object sender, EventArgs e)
{
//等号代码
richTextBox1.AppendText("=");
richTextBox1.Text = richTextBox1.Text;
richTextBox1.Text = DataOp.DataMain();
string temp = DataOp.DataMain();
inputStr.Clear();
for (int i = 0; i < temp.Length; i++)
{
inputStr.Add(temp[i]);
}
}
private void btn_Add_Click(object sender, EventArgs e)
{
inputStr.Add('+');
richTextBox1.AppendText("+");
}
private void btn_Subt_Click(object sender, EventArgs e)
{
inputStr.Add('-');
richTextBox1.AppendText("-");
}
private void btn_Mul_Click(object sender, EventArgs e)
{
inputStr.Add('×');
richTextBox1.AppendText("×");
}
private void btn_Divi_Click(object sender, EventArgs e)
{
inputStr.Add('÷');
richTextBox1.AppendText("÷");
}
private void btn_Ce_Click(object sender, EventArgs e)
{
richTextBox1.Text = "";
inputStr.Clear(); //清空链表的所有元素
}
private void btn_C_Click(object sender, EventArgs e)
{
//界面撤销
inputStr.RemoveAt(inputStr.Count - 1);
richTextBox1.Text = "";
for (int i = 0; i < inputStr.Count; i++)
{
richTextBox1.Text += inputStr[i];
}
}
美化设计主要是计算器背景优化和控件的透明度。使控件透明来陪衬背景,以此增加视觉效果,变得更美观。
而透明度更改只需更改控件属性即可,例如btn_1:
this.btn_1.BackColor = System.Drawing.Color.Transparent;
this.btn_1.FlatAppearance.BorderSize = 0;
this.btn_1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btn_1.Font = new System.Drawing.Font("仿宋", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.btn_1.ForeColor = System.Drawing.SystemColors.ButtonHighlight;
this.btn_1.Location = new System.Drawing.Point(16, 239);
this.btn_1.Margin = new System.Windows.Forms.Padding(4);
this.btn_1.Name = "btn_1";
this.btn_1.Size = new System.Drawing.Size(83, 45);
this.btn_1.TabIndex = 11;
this.btn_1.Text = "1";
this.btn_1.UseVisualStyleBackColor = false;
this.btn_1.Click += new System.EventHandler(this.btn_1_Click);
0-9数字由button控件控制运行,0-9数字的设计代码:
private void btn_0_Click(object sender, EventArgs e)
{
inputStr.Add('0');
richTextBox1.AppendText("0");
//1-9的代码以此类推
//显示屏由控件richTextBox构成。
//运算符号包括:+-/*和()运算符号代码:
private void btn_Left_Click(object sender, EventArgs e)
{
inputStr.Add('(');
richTextBox1.AppendText("(");
}
private void btn_Rig_Click(object sender, EventArgs e)
{
inputStr.Add(')');
richTextBox1.AppendText(")");
}
private void btn_Equal_Click(object sender, EventArgs e)
{
//等号代码
richTextBox1.AppendText("=");
richTextBox1.Text = richTextBox1.Text;
richTextBox1.Text = DataOp.DataMain();
string temp = DataOp.DataMain();
inputStr.Clear();
for (int i = 0; i < temp.Length; i++)
{
inputStr.Add(temp[i]);
}
}
private void btn_Add_Click(object sender, EventArgs e)
{
inputStr.Add('+');
richTextBox1.AppendText("+");
}
private void btn_Subt_Click(object sender, EventArgs e)
{
inputStr.Add('-');
richTextBox1.AppendText("-");
}
private void btn_Mul_Click(object sender, EventArgs e)
{
inputStr.Add('×');
richTextBox1.AppendText("×");
}
private void btn_Divi_Click(object sender, EventArgs e)
{
inputStr.Add('÷');
richTextBox1.AppendText("÷");
}
//界面的清空和撤销:
private void btn_Ce_Click(object sender, EventArgs e)
{
richTextBox1.Text = "";
inputStr.Clear(); //清空链表的所有元素
}
private void btn_C_Click(object sender, EventArgs e)
{
//界面撤销
inputStr.RemoveAt(inputStr.Count - 1);
richTextBox1.Text = "";
for (int i = 0; i < inputStr.Count; i++)
{
richTextBox1.Text += inputStr[i];
}
}