C# delegate 学习

/Files/longren629/DelegateDemo.zip

1:操作类

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;

/// <summary>
/// Demo 的摘要说明
/// </summary>

public   class  Demo
{
    
内部变量
    
构造函数
}


2:页面
<% @ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default"  %>

<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >

< html  xmlns ="http://www.w3.org/1999/xhtml"   >
< head  runat ="server" >
    
< title > Delegate学习实例 </ title >
</ head >
< body >
    
< form  id ="form1"  runat ="server" >
      
< asp:Button  ID ="Button1"  runat ="server"  Text ="添加成都同时利用Delegate添加四川"  OnClick ="Button1_Click"   />
        
< br  />
        
< asp:Button  ID ="Button2"  runat ="server"  Text ="移除四川同时利用Delegate移除成都"  OnClick ="Button2_Click"   />< br  />
        
< asp:ListBox  ID ="ListBox1"  runat ="server"  Width ="330px"  Height ="328px" ></ asp:ListBox >
      
       
   
    
</ form >
</ body >
</ html >

3,页面代码隐藏类
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 
{
    
protected void Page_Load(object sender, EventArgs e)
    
{

    }

    
protected void Button1_Click(object sender, EventArgs e)
    
{
        Demo bll 
= new Demo("四川""成都");
        bll.AddTown 
+= new Demo.AddTownDelegate(AddProvince);
        
this.ListBox1.Items.Add(bll.Town);
        bll.AddNewTown();   
    
    }

    
protected void AddProvince(string Province)
    
{
        
this.ListBox1.Items.Add(Province);
    }


    
protected void RemovedCity(string City)
    
{
        
this.ListBox1.Items.Remove(new ListItem(City));
    }


    
protected void Button2_Click(object sender, EventArgs e)
    
{
        Demo bll 
= new Demo("四川""成都");
        bll.RemoveProvince 
+= new Demo.RemoveProvinceDelegate(RemovedCity);
        
this.ListBox1.Items.Remove(new ListItem(bll.Province));
        bll.RemoveOldProvince();
    }

}

你可能感兴趣的:(delegate)