[原]把一个简单计算器做成Web自定义控件

 

这次,我们就继《动态创建Web控件制做计算器》的文章,把Web控件制作的就算器做成一个Web控件,首先,打开vs2005,文件/新建/项目,如下图:

 

visual C#中选择Windows 在模版里面选择Web控件库,输入名称和存储位置,点确定。进入代码区以后,删掉默认的Text属性,开始写我们的代码,先修该一些必要的属性,以至于让我们自己做的控件像一个产品。

    将控件类名字从默认的WebCustomControl1.cs改为CalculatorControl.cs,同时,代码里面的类名字也要改的和它一样,类上面的ToolboxData属性也要做响应的修改,改成[ToolboxData("<{0}:CalculatorControl runat=server></{0}:CalculatorControl>")],注意的是上面修改的三处的类名必须相同,如果不同写的控件将不能用。然后打开AssemblyInfo.cs文件,在命名空间中引入using System.Web.UI;在下面属性里加一个[assembly: TagPrefix("WebControlLibrary1", "liu")],其中第一个参数是工程的命名空间,第二个是控件以后拖动到网页上以后<>标签里面显示的名字,比如,我们第二个参数写成liu,控件拖动到网页上以后,就显示成<liu:CalculatorControl ID="CalculatorControl1" runat="server" />。完了以后可以根据需要,修改上面的公司名称,版权等属性做完这些工作开始写代码。

注意的是Web自定义控件的编写过程是不可视的,所有的控件、控件的事件都必须写代码来完成,初步规划编写的步骤:先写一个构造函数,在构造函数里面完成算器的界面的创建。然后再定义创建按钮的函数,和写按钮要响应的事件最后重写绘制的方法RenderContents。写完后,整个程序代码如下:

None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.ComponentModel;
None.gif
using  System.Text;
None.gif
using  System.Web;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
namespace  WebControlLibrary1
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    [ToolboxData(
"<{0}:CalculatorControl runat=server></{0}:CalculatorControl>")]
InBlock.gif    
public class CalculatorControl : WebControl
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//定义两个操作数
InBlock.gif
        static double Num1;
InBlock.gif        
static double Num2;
InBlock.gif        
//记录操作符
InBlock.gif
        static string OperatorF;
InBlock.gif        
//判断“=”是否被点击过,每次操作时清空上次操作的记录
InBlock.gif
        static bool ReOperator = false;
InBlock.gif        
//容器,容纳所有控件
InBlock.gif
        Panel CT = new Panel();
InBlock.gif        TextBox txtShow 
= new TextBox();
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 构造函数,在此函数里面动态的创建计算器的界面
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public CalculatorControl()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            txtShow.Width 
= 95;
InBlock.gif            CT.Controls.Add(txtShow);
InBlock.gif            HtmlGenericControl htmlString 
= new HtmlGenericControl();
InBlock.gif            htmlString.InnerHtml 
= "<br>";
InBlock.gif            CT.Controls.Add(htmlString);
InBlock.gif            
for (int i = 0, I = 1; i < 3; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
string[] Op = dot.gif"+""-""*" };
ExpandedSubBlockStart.gifContractedSubBlock.gif                
string[] CharOp = dot.gif"jia""jian""cheng" };
InBlock.gif                
for (int j = 0; j < 3; j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    CT.Controls.Add(CreateBtn(I.ToString()));
InBlock.gif                    I
++;
ExpandedSubBlockEnd.gif                }

InBlock.gif                Button OperBtn 
= new Button();
InBlock.gif                OperBtn.Text 
= Op[i];
InBlock.gif                OperBtn.ID 
= CharOp[i];
InBlock.gif                OperBtn.Width 
= 25;
InBlock.gif                OperBtn.Click 
+= new EventHandler(Btn_Click);
InBlock.gif                CT.Controls.Add(OperBtn);
InBlock.gif                HtmlGenericControl htmlStr 
= new HtmlGenericControl();
InBlock.gif                htmlStr.InnerHtml 
= "<br>";
InBlock.gif                CT.Controls.Add(htmlStr);
ExpandedSubBlockEnd.gif            }

InBlock.gif            CT.Controls.Add(CreateBtn(
"Backspace""B"));
InBlock.gif            CT.Controls.Add(CreateBtn(
"Clear""C"));
InBlock.gif            CT.Controls.Add(CreateBtn(
"deng""="));
InBlock.gif            CT.Controls.Add(CreateBtn(
"chu""/"));
InBlock.gif            
//自己定义的将控件添加到当前Web控件,没有这一步,上面创建的所有控件不会响应事件
InBlock.gif
            this.Controls.Add(CT);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void RenderContents(HtmlTextWriter output)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif           CT.RenderControl(output);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 创建操作数按钮的函数
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="Num">操作数</param>
ExpandedSubBlockEnd.gif        
/// <returns>返回一个按钮</returns>

InBlock.gif        public Button CreateBtn(string Num)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Button NumBtn 
= new Button();
InBlock.gif            NumBtn.Text 
= Num;
InBlock.gif            NumBtn.ID 
= "ID" + Num;
InBlock.gif            NumBtn.Width 
= 25;
InBlock.gif            NumBtn.Click 
+= new EventHandler(Btn_Click);
InBlock.gif            
return NumBtn;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 创建操作符按钮的函数
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="ID">按钮的ID号</param>
InBlock.gif        
/// <param name="Operator">操作符</param>
ExpandedSubBlockEnd.gif        
/// <returns>返回一个按钮</returns>

InBlock.gif        public Button CreateBtn(string ID, string Operator)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Button OperBtn 
= new Button();
InBlock.gif            OperBtn.Text 
= Operator;
InBlock.gif            OperBtn.ID 
= ID;
InBlock.gif            OperBtn.Width 
= 25;
InBlock.gif            OperBtn.Click 
+= new EventHandler(Btn_Click);
InBlock.gif            
return OperBtn;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 定义按钮响应的事件,所有按钮通用一个事件,用按钮的Text属性区分要执行的操作
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="sender"></param>
ExpandedSubBlockEnd.gif        
/// <param name="e"></param>

InBlock.gif        public void Btn_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string BtnText = ((Button)sender).Text;
InBlock.gif            
switch (BtnText)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
case "+":
InBlock.gif                    Num1 
= int.Parse(txtShow.Text);
InBlock.gif                    OperatorF 
= BtnText;
InBlock.gif                    txtShow.Text 
= "";
InBlock.gif                    
break;
InBlock.gif                
case "-":
InBlock.gif                    Num1 
= int.Parse(txtShow.Text);
InBlock.gif                    OperatorF 
= BtnText;
InBlock.gif                    txtShow.Text 
= "";
InBlock.gif                    
break;
InBlock.gif                
case "*":
InBlock.gif                    Num1 
= int.Parse(txtShow.Text);
InBlock.gif                    OperatorF 
= BtnText;
InBlock.gif                    txtShow.Text 
= "";
InBlock.gif                    
break;
InBlock.gif                
case "/":
InBlock.gif                    Num1 
= int.Parse(txtShow.Text);
InBlock.gif                    OperatorF 
= BtnText;
InBlock.gif                    txtShow.Text 
= "";
InBlock.gif                    
break;
InBlock.gif                
case "=":
InBlock.gif                    
if (OperatorF == "+")
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        Num2 
= Num1 + double.Parse(txtShow.Text);
InBlock.gif                        txtShow.Text 
= Num2.ToString();
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else if (OperatorF == "-")
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        Num2 
= Num1 - double.Parse(txtShow.Text);
InBlock.gif                        txtShow.Text 
= Num2.ToString();
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else if (OperatorF == "*")
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        Num2 
= Num1 * double.Parse(txtShow.Text);
InBlock.gif                        txtShow.Text 
= Num2.ToString();
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else if (OperatorF == "/")
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        Num2 
= Num1 / double.Parse(txtShow.Text);
InBlock.gif                        txtShow.Text 
= Num2.ToString();
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    ReOperator 
= true;
InBlock.gif                    
break;
InBlock.gif                
case "C":
InBlock.gif                    txtShow.Text 
= "";
InBlock.gif                    Num1 
= Num2 = 0;
InBlock.gif                    
break;
InBlock.gif                
case "B":
InBlock.gif                    
if (txtShow.Text.Length >= 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        txtShow.Text 
= txtShow.Text.Substring(0, txtShow.Text.Length - 1);
InBlock.gif                        
if (txtShow.Text != "")
InBlock.gif                            Num1 
= int.Parse(txtShow.Text);
InBlock.gif                        
else
InBlock.gif                            Num1 
= 0;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
break;
InBlock.gif                
default:
InBlock.gif                    
if (ReOperator)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        txtShow.Text 
= "";
InBlock.gif                        Num1 
= Num2 = 0;
InBlock.gif                        ReOperator 
= false;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    txtShow.Text 
+= BtnText;
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

和上一篇文章相比,这次我们在布局上减少了很多代码,这次没有用表格进行布局,而是创建了一个panel控件作为容器,然后把所有的控件都放在它的上面,引入了System.Web.UI.HtmlControls命名空间,用它里面的方法控制换行。Web控件就编写完了,先生成一下,再建立一个网页进行测试。

在解决方案上面单击右键,添加/新建网站,如图:

 

选择ASP.NET网站,选好存储路径,语言选C#,确定。进入页面以后,将网页设为起始页,在工具箱上单击右键/选择项,打开对话框,如下图:

点击浏览,选择我们刚生成的控件,点击确定,就将控件添加进工具箱了,如图:

然后将控件拖放到网页上面,网页代码如下:

ExpandedBlockStart.gif ContractedBlock.gif <% dot.gif @ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default"  %>
ExpandedBlockStart.gifContractedBlock.gif
<% dot.gif @ Register Assembly="Calculator" Namespace="WebControlLibrary1" TagPrefix="liu"  %>
None.gif
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
None.gif
< html  xmlns ="http://www.w3.org/1999/xhtml"   >
None.gif
< head  runat ="server" >
None.gif    
< title > 测试计算器控件 </ title >
None.gif
</ head >
None.gif
< body >
None.gif    
< form  id ="form1"  runat ="server" >
None.gif    
< div >
None.gif        
< liu:CalculatorControl  ID ="CalculatorControl1"  runat ="server"   />
None.gif       
</ div >
None.gif    
</ form >
None.gif
</ body >
None.gif
</ html >
None.gif

运行进行测试,结果如下图:

这样一个Web自定义控件就制作完成了,虽然这个控件的功能很不完整,甚至连0 都没有,但是我在制作过程,还是遇到了一些困难,不如:更改控件拖放到网页上标签的名字,控件无法响应事件等等,解决了问题,也学到一些东西,不足的地方大家有兴趣可以完善。

转载于:https://www.cnblogs.com/salonliudong/archive/2007/06/04/771295.html

猜你喜欢

转载自blog.csdn.net/weixin_33816821/article/details/93290931