Microsoft Visual Studio(简称VS)是美国微软公司的开发工具包系列产品。VS是一个基本完整的开发工具集,它包括了整个软件生命周期中所需要的大部分工具,如UML工具、代码管控工具、集成开发环境(IDE)等等。所写的目标代码适用于微软支持的所有平台,包括Microsoft Windows、Windows Mobile、Windows CE、.NET Framework、.NET Compact Framework和Microsoft Silverlight 及Windows Phone。
以下分享我是如何使用Visual Studio制作一个简单的计算器
目录
步骤 1: 打开Visual Studio并创建新项目
- 打开Visual Studio。
- 选择“创建新项目”。
- 在“创建新项目”对话框中,选择“Windows Forms App (.NET Framework)”(如果你使用的是较旧的Visual Studio版本)或“Windows Forms App (.NET)”(如果你使用的是较新的.NET Core或.NET 5/6/7等版本)。
- 输入项目名称并选择一个保存位置,然后点击“创建”。
步骤 2: 设计界面
在“设计”视图中,你会看到一个默认的窗体(Form)。
- 从工具箱(Toolbox)中拖动以下控件到窗体上:
1.一个TextBox
控件用于显示输入和结果。(勾选MultiLine)
2.一些Button
控件用于数字(0-9)和运算符(+、-、*、/)
3.在属性中修改Text显示的数字
4.一个Button
控件用于清除文本框(通常标记为“C”或“CE”)
5.一个Button
控件用于执行计算(通常标记为“=”)。
你可以通过拖拽控件来调整它们的大小和位置,
也可以通过属性窗口(Properties Window)来设置控件的属性(如Name、Text、Font等)。
步骤 3: 编写代码
双击按钮控件,Visual Studio会自动为你创建一个该按钮点击事件的事件处理程序。例如,双击数字按钮“1”将在Form1.cs
(或你的窗体名称)中生成一个名为button1_Click
的事件处理程序。
-
在事件处理程序中编写代码以处理按钮点击事件。例如,对于数字按钮,你可能希望将按钮的标签(Text)添加到文本框的文本末尾。
private void button1_Click(object sender, EventArgs e) { this.textBox1.Text += "1"; }
- 对于运算符按钮,你可能需要存储当前输入和运算符,以便稍后进行计算。你可以使用类的字段或属性来存储这些信息。
- 对于“=”按钮,编写代码以执行计算并将结果显示在文本框中。你可能需要使用
double.TryParse
或类似的方法来解析文本框中的文本为数字,并执行相应的计算。 - 对于清除按钮,编写代码以清除文本框的内容。
步骤 4: 测试和调试
- 点击工具栏上的“启动”按钮(通常是一个绿色的三角形图标)来编译并运行你的程序。
- 在程序窗口中测试你的计算器功能。测试不同的输入和运算符组合,确保计算器按预期工作。
- 如果发现任何问题或错误,使用Visual Studio的调试工具进行调试和修改。
【以下是完整代码】 :
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 计算机
{
public partial class FormCalculator2 : Form
{
#region 方法二:对表达式分析
string Precede(string p, string q)
{
switch (p)
{
case "+":
case "-":
return ("*/(".IndexOf(q) != -1) ? "<" : ">";
case "*":
case "/":
return (q == "(") ? "<" : ">";
case "(":
return (q == ")") ? "=" : "<";
case ")":
return (q == "(") ? "?" : ">";
case "#":
return (q == "#") ? "=" : "<";
}
return "?";
}
Double Operate(Double a, char o, Double b)
{
switch (o)
{
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
return a / b;
}
return 0;
}
public Object Eval_2(string Expression)
{
Stack nArr = new Stack(), oArr = new Stack();
int j = 0;
Double a = 0, b = 0;
string w = "";
char o;
MatchCollection arr = Regex.Matches(Expression.Replace(" ", "") + "#", @"(((?<=(^|\())-)?\d+(\.\d+)?|\D)");
oArr.Push('#');
w = Convert.ToString(arr[j++]);
while (!(w == "#" && Convert.ToString(oArr.Peek()) == "#"))
{
if ("+-*/()#".IndexOf(w) != -1)
{
switch (Precede(oArr.Peek().ToString(), w))
{
case "<":
oArr.Push(w);
w = Convert.ToString(arr[j++]);
break;
case "=":
oArr.Pop();
w = Convert.ToString(arr[j++]);
break;
case ">":
o = Convert.ToChar(oArr.Pop());
b = Convert.ToDouble(nArr.Pop());
a = Convert.ToDouble(nArr.Pop());
nArr.Push(Operate(a, o, b));
break;
default:
return "Error";
}
}
else
{
nArr.Push(w);
w = Convert.ToString(arr[j++]);
}
}
return nArr.Pop();
}
#endregion
public FormCalculator2()
{
InitializeComponent();
//无边框代码
// this.FormBorderStyle = FormBorderStyle.None;
this.MaximizeBox = false;
}
private void button1_Click(object sender, EventArgs e)
{
this.textBox1.Text += "1";
}
private void button2_Click(object sender, EventArgs e)
{
this.textBox1.Text += "2";
}
private void button3_Click(object sender, EventArgs e)
{
this.textBox1.Text += "3";
}
private void button4_Click(object sender, EventArgs e)
{
this.textBox1.Text += "4";
}
private void button6_Click(object sender, EventArgs e)
{
this.textBox1.Text += "5";
}
private void button7_Click(object sender, EventArgs e)
{
this.textBox1.Text += "6";
}
private void button8_Click(object sender, EventArgs e)
{
this.textBox1.Text += "7";
}
private void button9_Click(object sender, EventArgs e)
{
this.textBox1.Text += "8";
}
private void button11_Click(object sender, EventArgs e)
{
this.textBox1.Text += "9";
}
private void button12_Click(object sender, EventArgs e)
{
this.textBox1.Text += "0";
}
private void button5_Click(object sender, EventArgs e)
{
this.textBox1.Text += "+";
}
private void button10_Click(object sender, EventArgs e)
{
this.textBox1.Text += "-";
}
private void button15_Click(object sender, EventArgs e)
{
this.textBox1.Text += "*";
}
private void button20_Click(object sender, EventArgs e)
{
this.textBox1.Text += "/";
}
private void button13_Click(object sender, EventArgs e)
{
this.textBox1.Text += ".";
}
private void button17_Click(object sender, EventArgs e)
{
this.textBox1.Text += "(";
}
private void button18_Click(object sender, EventArgs e)
{
this.textBox1.Text += ")";
}
private void button16_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
}
//计算结果
private void button19_Click(object sender, EventArgs e)
{
string str=this.textBox1.Text;
this.textBox1.Text = Eval_2(str).ToString();
}
private void FormCalculator2_Load(object sender, EventArgs e)
{
}
}
}
步骤 5:(可选)添加更多功能
你可以继续扩展你的计算器应用,添加更多功能
- 考虑添加更多功能,如错误处理(例如,尝试除以零时显示错误消息)。
- 添加支持更多运算符(如括号、百分比等)。
- 改进用户界面,使其更易于使用和理解。
- 考虑使用更高级的技术,如数据绑定、事件聚合等,以提高代码的可维护性和可重用性。