小宋

song_xiaopeng@126.com

导航

C#关于委托。。

制作人:小宋

编码人:小宋

制作时间:2010年9月8日17:00:57

c#用委托来实现事件通知机制。委托相当与c++函数指针。

     前段时间因为系统要更新,要把原先的【选择业务】下拉列表框更换为按钮点击后弹出窗体选择业务等一系列操作。

我接到任务后同事说你可以用下c#委托,刚开始我在网上看了些教程不太明白 - -!  后来经过同事一讲解,哇!豁然开朗~~~再此感谢下这位同事。

- 具体实现

页面描述:A页面有个选择业务按钮

          B页面有12个RadioButton,分别代表了12个不同的qq业务,选择一个业务点击确定按钮。

注:B页面所有的RadioButton项的Tag值都与A页面里的【充值类型】相对应,并且公用一个rdo_CheckedChanged事件。

现在要做的是,点击A页面的【选择业务】进入B页面,选择业务后点击确定关闭当前窗体并把选中的业务传到A页面。如果A页面【充值类型】为B页面中的其中一个时,进入B页面时加载窗体并选中A页面显示的业务类型。

A页面代码:

申明委托 ,在包里或者类里,public

 

代码
//定义一个委托
public delegate void QQType(string type);

//声明一个结构
private struct GameType
{
public string type;
public decimal unitPrice;
public string Desc;

public GameType(string gameType, decimal gameUnitPrice,string gameDesc)
{
this.type = gameType;
this.unitPrice = gameUnitPrice;
this.Desc = gameDesc;
}
public override string ToString()
{
return type;
}
}

// 放置用于绑定 ComboBox 的数据集合
private List<GameType> _typeList = new List<GameType>();
//加载
private void QQ_Load(object sender, EventArgs e)
{
this._typeList.Add(new GameType("QQ币充值", 1.0m, "Q币可购买腾讯所有业务及游戏道具等,1元 = 1 Q币"));
this._typeList.Add(new GameType("QQ会员", 10.0m ,"QQ会员为您提供等级加速、游戏礼包、电影票等50余项QQ、游戏、生活三大领域特权体验。10元/月"));
this._typeList.Add(new GameType("QQ红钻", 10.0m, "QQ秀红钻贵族,5万件QQ秀免费穿、免费送!还能免费送鲜花!10元/月"));
this._typeList.Add(new GameType("QQ黄钻", 10.0m, "黄钻贵族是QQ空间尊贵用户,享有QQ空间装扮免费任用、超大无压缩相册空间、尊贵身份标识、应用与功能抢先体验等超过30项精彩功能特权!10元/月"));
this._typeList.Add(new GameType("QQ蓝钻", 10.0m, "QQ游戏踢人、防踢,还有双倍积分等特权!10元/月"));
this._typeList.Add(new GameType("QQ绿钻", 10.0m, "百万空间背景音乐免费用!还可上传本地音乐,观看高清音乐视频!下载高品质mp3!10元/月"));
this._typeList.Add(new GameType("QQ音速紫钻", 10.0m, "QQ音速踢人、防踢,享受音速疯狂折扣等特权!10元/月"));
this._typeList.Add(new GameType("QQ堂紫钻", 10.0m, "QQ堂VIP角色和道具独享,还有防踢等特权!10元/月"));
this._typeList.Add(new GameType("QQ飞车紫钻", 10.0m, "QQ飞车踢人防踢,经验加成,专属道具与任务疯狂折扣等特权!10元/月"));
this._typeList.Add(new GameType("QQ炫舞紫钻", 20.0m, "QQ炫舞踢人防踢功、对局经验独享加成、紫钻贵族礼包免费送等特权!20元/月"));
this._typeList.Add(new GameType("QQ粉钻", 10.0m, "QQ宠物购物8.8折优惠,交通工具随便用等特权!10元/月"));
this._typeList.Add(new GameType("QQ黑钻", 20.0m, "地下城与勇士黑钻贵族,疲劳上限增加、通关经验奖励、通关翻牌奖励和尊贵的游戏标识!20元/月"));

this.cboType.DataSource = this._typeList;
//业务控件不可用
this.cboType.Enabled = false;
this.cboType.SelectedIndex = 0;
}


//把之前定义的委托实现一下(返回类型、参数类型必须一致)
private void QQtype(string typeId)
{
this.cboType.Text = typeId; //把从B页面获取到的【业务类型】赋给【充值类型】
GameType type = (GameType)(this.cboType.SelectedValue);
if ("QQ币充值" != type.type)
{
//执行QQ币充值操作
}
else
{
this.lblPriceType.Text = "充值金额:";
this.lblTotalPrice.Visible = false;
this._totalPrice = decimal.Truncate(this.numericUpDown1.Value);
}
}

//选择业务
private void button7_Click(object sender, EventArgs e)
{
QQType qt
= new QQType(QQtype);
Option option
= new Option(qt);
option.Name
= cboType.SelectedValue.ToString();
option.ShowDialog();
}

 

B页面代码:

 

代码
//保存一个RadioButton的Tag属性值
public string Name { get; set; }
//定义RadioButton 的Tag值
private string radioTag;
private QQType _qqType;

//构造函数 :this() ->(执行这个函数前先执行无参的函数)
public Option(QQType qqType)
:
this()
{
this._qqType = qqType;
}

//确定按钮
private void button1_Click(object sender, EventArgs e)
{
this._qqType(radioTag);
this.Close();
}

//选中RadioButton 触发
private void rdo_CheckedChanged(object sender, EventArgs e)
{
RadioButton rb
= sender as RadioButton;
//获取当前RadioButton的Tag值
this.radioTag = rb.Tag.ToString();
}
//加载
private void Option_Load(object sender, EventArgs e)
{
this.Size = new Size(458, 274);
//根据A页面传过来的【充值类型】(RadioButton的Tag属性值) 来设置业务被选中
//所有的RadioButton都放在panel1里面
for (int i = 0; i < panel1.Controls.Count; i++)
{
RadioButton rb
= panel1.Controls[i] as RadioButton;
if (rb.Tag.ToString() == Name) //Name:A页面传过来的【充值类型】Tag 属性值
{
rb.Checked
= true;
}
}
}

 

朋友,看过之后不妨留下你的建议或者见解;如要转载本文请标明原文地址http://www.cnblogs.com/song_ 谢谢!

posted on 2010-09-20 17:16  _小宋  阅读(1125)  评论(4编辑  收藏  举报