用户名: 密码: 验证码:           网站地图 高级搜索 RSS订阅 收藏本站
Google
您的位置:首页>>网络编程>>.Net编程>>阅读资讯:一个C#高级编程中的事件 DEMO 实例

一个C#高级编程中的事件 DEMO 实例

[ 来源: | 阅读:次 | 更新日期:2007-10-15 23:58:14 | 评论 0 条 | 我要投稿 ]

  一个C#高级编程中的事件 DEMO 实例,C#高级编程中的例子,讲的是事件DEMO,一起来看看吧。

字串5

using System;
  using System.Collections.Generic;
  using System.Text;
  namespace ConsoleApplication1
  {
  class Program
  {
  /// 
  /// 发生一个事件:用户选择了一个选项
  /// 响应是1:用户得到想要的结果
  /// 2:老板得到是否有用户在看他的信息
  ///
  /// 即:多点委托[显示结果,提示老板]
  ///
  /// 
  static void Main(string[] args)
  {
  //UserInputMonitor 的成员userInputRequest 事件
  UserInputMonitor uim = new UserInputMonitor();
  //为userInputRequest绑定2个方法
  UserView uv = new UserView(uim);
  person p = new person(uim);
  //run方法里面会提示用户来促发userInputRequest 事件
  uim.Run();
  }
  }
  }
  using System;
  using System.Collections.Generic;
  using System.Text;
  namespace ConsoleApplication1
  {

字串9

  class UserInputMonitor
  {
  public delegate void userInputRequest(object send,UserEventArgs e);
  public event userInputRequest onUserInputRequest;
  public void Run()
  {
  //不同的促发情况
  //UserEventArgs 带上了用户选择的信息
  while (true)
  {
  System.Console.WriteLine("input p:[look personal ]");
  System.Console.WriteLine("input c:[look company]");
  System.Console.WriteLine("input e:[exit ]");
  string input = System.Console.ReadLine();
  if (input.ToLower() == "p")
  onUserInputRequest(this, new UserEventArgs(RequestType.Personal));
  else if (input.ToLower() == "c")
  onUserInputRequest(this, new UserEventArgs(RequestType.Company));
  else if (input.ToLower() == "e")
  return;
  else
  continue;
  }
  }
  }
  }
  using System; 字串9
  using System.Collections.Generic;
  using System.Text;
  namespace ConsoleApplication1
  {
  enum RequestType{
  Personal,
  Company
  }
  /// 
  /// 携带了用户选择信息的EventArgs
  /// 
  class UserEventArgs : EventArgs
  {
  private RequestType reqType;
  public RequestType GetRequestType
  {
  get
  {
  return this.reqType;
  }
  }
  public UserEventArgs(RequestType t)
  {
  this.reqType = t;
  }
  }
  }
  using System;
  using System.Collections.Generic;
  using System.Text;
  namespace ConsoleApplication1
  {
  /// 
  /// 绑定一个事件
  /// 
  class UserView
  {
  public UserView(UserInputMonitor uim)
  {
  uim.onUserInputRequest += new UserInputMonitor.userInputRequest(uim_onUserInputRequest);

字串7


  }
  protected void uim_onUserInputRequest(object send, UserEventArgs e)
  {
  if (e.GetRequestType == RequestType.Company)
  System.Console.WriteLine("company is best!!");
  if(e.GetRequestType == RequestType.Personal)
  System.Console.WriteLine("he is chinese!!");
  }
  }
  }
  using System;
  using System.Collections.Generic;
  using System.Text;
  namespace ConsoleApplication1
  {
  /// 
  /// 绑定一个事件
  /// 
  class person
  {
  public person(UserInputMonitor uim)
  {
  uim.onUserInputRequest += new UserInputMonitor.userInputRequest(uim_onUserInputRequest);
  }
  protected void uim_onUserInputRequest(object send, UserEventArgs e)
  {
  if (e.GetRequestType == RequestType.Personal)
  System.Console.WriteLine("somebody miss u!!");
  } 字串8
  }
  }
字串6


Tags:C#,高级编程,事件,DEMO
责任编辑:
您的评论
用户名:新注册) 密码: 匿名评论 [所有评论]

·用户发表意见仅代表其个人意见,并且承担一切因发表内容引起的纠纷和责任
·本站管理人员有权在不通知用户的情况下删除不符合规定的评论信息或留做证据
·请客观的评价您所看到的资讯,提倡就事论事,杜绝漫骂和人身攻击等不文明行为