使用vs2003或是vs2005来编写一个简单的计算器程序是非常简单的,直接从工具框中拽几个控件到窗体上,然后双击控件写几行事件处理代码就OK了。那么怎样脱离IED环境来写这么一个简单的程序呢?请接着往下看
首先写一个叫Interface的类来定义窗口的外观
//Interface.cs
using System;
using System.Windows.Forms;
using System.Drawing;
public class Interface:Forms
{ private TextBox numberOneTextBox;
private TextBox numberTwoTextBox;
private TextBox resultTextBox;
private Button addButton;
private Button subButton;
private Button mulButton;
private Button divButton;
public Interface()//构造函数
{
this.Height=300;//定义窗体的大小
this.Width=300;
this.StartPosition=FormStartPosition.CenterScreen;//窗体在桌面中间
this.Visible=true;//让窗体可见
this.MinimumSize=this.Size;//让窗体的最小大小为当前大小
numberOneTextBox=new TextBox();
numberOneTextBox.Width=20;
numberOneTextBox.Left=15;
numberOneTextBox.AutoSize=true;
numberTwoTextBox=new TextBox();
numberTwoTextBox.Width=20;
numberTwoTextBox.left=numberTwoTextBox.Right+15;
numberTwoTextBox.AutoSize=true;
resultTextBox=new TextBox();
resultTextBox.Width=20;
resultTextBox.left=15;
resultTextBox.AutoSize=true;
int buttonHeight=50;
int buttonWidth=80;
addButton=new Button();//加按钮
addButton.Height=buttonHeight;
addButton.Width=buttonWidth;
addButton.Top=numberOneTextBox.Buttom+20;
addButton.Left=15;
addButton.Text="add";
subButton=new Button();//减按钮
subButton.Height=buttonHeight;
subButton.Width=buttonWidth;
subButton.Top=numberOneTextBox.Buttom+20;
subButton.Left=addButton.Right+15;
subButton.Text="sub";
mulButton=new Button();//乘按钮
mulButton.Height=buttonHeight;
mulButton.Width=buttonWidth;
mulButton.Top=numberOneTextBox.Buttom+20;
mulButton.Left=subButton.Right+15;
mulButton.Text="mul";
divButton=new Button();//除按钮
divButton.Height=buttonHeight;
divButton.Width=buttonWidth;
divButton.Top=numberOneTextBox.Buttom+20;
divButton.Left=mulButton.Right+15;
divButton.Text="div";
this.Control.Add(numberOneTextBox);//将控件添加到窗体
this.Control.Add(numberTwoTextBox);
this.Control.Add(resultTextBox);
this.Control.Add(addButton);
this.Control.Add(subButton);
this.Control.Add(mulButton);
this.Control.Add(divButton);
}
}
在上面的类中并没有主函数,现在来写一个Calculator类来加载刚才的控件
//Calculator.cs
using System;
using System.Widows.Forms;
public class Calculatot
{
static void Main()
{
Application.Run(new Interface());
}
}
到这儿整个界面就完成了,但现在还不能实现计算的功能,下一篇完善这段代码让其能实现计算功能。
脱离IDE的一个简单C#程序(上)
最新推荐文章于 2021-02-28 02:54:21 发布