最近参加了Web基础开发的培训,收获不少,做了一个练习,在后台动态创建控件制作了一个简单功能的计算器。程序中控件创建好以后,往往不能放在想要的位置,前台的布局非常麻烦,我用Table、TableRow、TableCell对象来帮助前台页面的布局。在创建控件前,将要创建的控件分类,按照分类,用重载的方法自己定义了CreateBtn函数。然后用循环调用的方法进行创建。整个程序的代码如下:
前台HTML代码(VS2005自动生成的,我没做任何修改);
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>网页动态生成计算器</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>网页动态生成计算器</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
后台Asp.net代码:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
//定义两个操作数
static double Num1;
static double Num2;
//记录操作符
static string OperatorF;
//判断“=”是否被点击过,每次操作时清空上次操作的记录
static bool ReOperator = false;
TextBox txtShow = new TextBox();
/// <summary>
/// 在页面登录事件里面创建计算器的界面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
//定义表格用于容纳其它创建的控件
Table txtTB = new Table();
form1.Controls.Add(txtTB);
txtShow.Width = 110;
TableRow TR = new TableRow();
TableCell TC = new TableCell();
TC.Controls.Add(txtShow);
TR.Controls.Add(TC);
txtTB.Controls.Add(TR);
Table TB = new Table();
form1.Controls.Add(TB);
//循环创建有规律分布的数字控件,和操作符控件
for (int i = 0,I = 1; i < 3; i++)
{
//用两个数组记录操作符控件的Text和ID值
string[] Op = {"+","-","*"};
string[] CharOp = {"jia","jian","cheng"};
TableRow TRows = new TableRow();
TB.Controls.Add(TRows);
for (int j = 0; j < 3; j++)
{
TableCell Tcell = new TableCell();
Tcell.Controls.Add(CreateBtn(I.ToString()));
I++;
TRows.Controls.Add(Tcell);
}
TableCell TcellO = new TableCell();
Button OperBtn = new Button();
OperBtn.Text = Op[i];
OperBtn.ID = CharOp[i];
OperBtn.Width = 25;
OperBtn.Click += new EventHandler(Btn_Click);
TcellO.Controls.Add(OperBtn);
TRows.Controls.Add(TcellO);
}
TableRow TRow = new TableRow();
TB.Controls.Add(TRow);
TableCell Tc2 = new TableCell();
Tc2.Controls.Add(CreateBtn("Backspace","B"));
TRow.Controls.Add(Tc2);
TableCell Tc3 = new TableCell();
Tc3.Controls.Add(CreateBtn("Clear","C"));
TRow.Controls.Add(Tc3);
TableCell Tc1 = new TableCell();
Tc1.Controls.Add(CreateBtn("deng", "="));
TRow.Controls.Add(Tc1);
TableCell Tc = new TableCell();
Tc.Controls.Add(CreateBtn("chu", "/"));
TRow.Controls.Add(Tc);
}
/// <summary>
/// 创建操作数按钮的函数
/// </summary>
/// <param name="Num">操作数</param>
/// <returns>返回一个按钮</returns>
protected Button CreateBtn(string Num)
{
Button NumBtn = new Button();
NumBtn.Text = Num;
NumBtn.ID = "ID" + Num;
NumBtn.Width = 25;
NumBtn.Click += new EventHandler(Btn_Click);
return NumBtn;
}
/// <summary>
/// 创建操作符按钮的函数
/// </summary>
/// <param name="ID">按钮的ID号</param>
/// <param name="Operator">操作符</param>
/// <returns>返回一个按钮</returns>
private Button CreateBtn(string ID, string Operator)
{
Button OperBtn = new Button();
OperBtn.Text = Operator;
OperBtn.ID = ID;
OperBtn.Width = 25;
OperBtn.Click += new EventHandler(Btn_Click);
return OperBtn;
}
/// <summary>
/// 定义按钮响应的事件,所有按钮通用一个事件,用按钮的Text属性区分要执行的操作
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Btn_Click(object sender, EventArgs e)
{
string BtnText = ((Button)sender).Text;
switch(BtnText)
{
case "+":
Num1 = int.Parse(txtShow.Text);
OperatorF = BtnText;
txtShow.Text = "";
break;
case "-":
Num1 = int.Parse(txtShow.Text);
OperatorF = BtnText;
txtShow.Text = "";
break;
case "*":
Num1 = int.Parse(txtShow.Text);
OperatorF = BtnText;
txtShow.Text = "";
break;
case "/":
Num1 = int.Parse(txtShow.Text);
OperatorF = BtnText;
txtShow.Text = "";
break;
case "=":
if (OperatorF == "+")
{
Num2 = Num1 + double.Parse(txtShow.Text);
txtShow.Text = Num2.ToString();
}
else if (OperatorF == "-")
{
Num2 = Num1 - double.Parse(txtShow.Text);
txtShow.Text = Num2.ToString();
}
else if (OperatorF == "*")
{
Num2 = Num1 * double.Parse(txtShow.Text);
txtShow.Text = Num2.ToString();
}
else if (OperatorF == "/")
{
Num2 = Num1 / double.Parse(txtShow.Text);
txtShow.Text = Num2.ToString();
}
ReOperator = true;
break;
case "C":
txtShow.Text = "";
Num1 = Num2 = 0;
break;
case "B":
if (txtShow.Text.Length >=1)
{
txtShow.Text = txtShow.Text.Substring(0, txtShow.Text.Length - 1);
if (txtShow.Text != "")
Num1 = int.Parse(txtShow.Text);
else
Num1 = 0;
}
break;
default:
if (ReOperator)
{
txtShow.Text = "";
Num1 = Num2 = 0;
ReOperator = false;
}
txtShow.Text += BtnText;
break;
}
}
}
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
//定义两个操作数
static double Num1;
static double Num2;
//记录操作符
static string OperatorF;
//判断“=”是否被点击过,每次操作时清空上次操作的记录
static bool ReOperator = false;
TextBox txtShow = new TextBox();
/// <summary>
/// 在页面登录事件里面创建计算器的界面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
//定义表格用于容纳其它创建的控件
Table txtTB = new Table();
form1.Controls.Add(txtTB);
txtShow.Width = 110;
TableRow TR = new TableRow();
TableCell TC = new TableCell();
TC.Controls.Add(txtShow);
TR.Controls.Add(TC);
txtTB.Controls.Add(TR);
Table TB = new Table();
form1.Controls.Add(TB);
//循环创建有规律分布的数字控件,和操作符控件
for (int i = 0,I = 1; i < 3; i++)
{
//用两个数组记录操作符控件的Text和ID值
string[] Op = {"+","-","*"};
string[] CharOp = {"jia","jian","cheng"};
TableRow TRows = new TableRow();
TB.Controls.Add(TRows);
for (int j = 0; j < 3; j++)
{
TableCell Tcell = new TableCell();
Tcell.Controls.Add(CreateBtn(I.ToString()));
I++;
TRows.Controls.Add(Tcell);
}
TableCell TcellO = new TableCell();
Button OperBtn = new Button();
OperBtn.Text = Op[i];
OperBtn.ID = CharOp[i];
OperBtn.Width = 25;
OperBtn.Click += new EventHandler(Btn_Click);
TcellO.Controls.Add(OperBtn);
TRows.Controls.Add(TcellO);
}
TableRow TRow = new TableRow();
TB.Controls.Add(TRow);
TableCell Tc2 = new TableCell();
Tc2.Controls.Add(CreateBtn("Backspace","B"));
TRow.Controls.Add(Tc2);
TableCell Tc3 = new TableCell();
Tc3.Controls.Add(CreateBtn("Clear","C"));
TRow.Controls.Add(Tc3);
TableCell Tc1 = new TableCell();
Tc1.Controls.Add(CreateBtn("deng", "="));
TRow.Controls.Add(Tc1);
TableCell Tc = new TableCell();
Tc.Controls.Add(CreateBtn("chu", "/"));
TRow.Controls.Add(Tc);
}
/// <summary>
/// 创建操作数按钮的函数
/// </summary>
/// <param name="Num">操作数</param>
/// <returns>返回一个按钮</returns>
protected Button CreateBtn(string Num)
{
Button NumBtn = new Button();
NumBtn.Text = Num;
NumBtn.ID = "ID" + Num;
NumBtn.Width = 25;
NumBtn.Click += new EventHandler(Btn_Click);
return NumBtn;
}
/// <summary>
/// 创建操作符按钮的函数
/// </summary>
/// <param name="ID">按钮的ID号</param>
/// <param name="Operator">操作符</param>
/// <returns>返回一个按钮</returns>
private Button CreateBtn(string ID, string Operator)
{
Button OperBtn = new Button();
OperBtn.Text = Operator;
OperBtn.ID = ID;
OperBtn.Width = 25;
OperBtn.Click += new EventHandler(Btn_Click);
return OperBtn;
}
/// <summary>
/// 定义按钮响应的事件,所有按钮通用一个事件,用按钮的Text属性区分要执行的操作
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Btn_Click(object sender, EventArgs e)
{
string BtnText = ((Button)sender).Text;
switch(BtnText)
{
case "+":
Num1 = int.Parse(txtShow.Text);
OperatorF = BtnText;
txtShow.Text = "";
break;
case "-":
Num1 = int.Parse(txtShow.Text);
OperatorF = BtnText;
txtShow.Text = "";
break;
case "*":
Num1 = int.Parse(txtShow.Text);
OperatorF = BtnText;
txtShow.Text = "";
break;
case "/":
Num1 = int.Parse(txtShow.Text);
OperatorF = BtnText;
txtShow.Text = "";
break;
case "=":
if (OperatorF == "+")
{
Num2 = Num1 + double.Parse(txtShow.Text);
txtShow.Text = Num2.ToString();
}
else if (OperatorF == "-")
{
Num2 = Num1 - double.Parse(txtShow.Text);
txtShow.Text = Num2.ToString();
}
else if (OperatorF == "*")
{
Num2 = Num1 * double.Parse(txtShow.Text);
txtShow.Text = Num2.ToString();
}
else if (OperatorF == "/")
{
Num2 = Num1 / double.Parse(txtShow.Text);
txtShow.Text = Num2.ToString();
}
ReOperator = true;
break;
case "C":
txtShow.Text = "";
Num1 = Num2 = 0;
break;
case "B":
if (txtShow.Text.Length >=1)
{
txtShow.Text = txtShow.Text.Substring(0, txtShow.Text.Length - 1);
if (txtShow.Text != "")
Num1 = int.Parse(txtShow.Text);
else
Num1 = 0;
}
break;
default:
if (ReOperator)
{
txtShow.Text = "";
Num1 = Num2 = 0;
ReOperator = false;
}
txtShow.Text += BtnText;
break;
}
}
}
整个程序的运行结果如下:
可以将这个计算器做成一个自定义Web控件,代码的改动不大,以后有机会了再讲,程序中做的计算器的功能还不完整,有的地方控制还不够,广大网友有兴趣还可以自己完善扩展。