WinForm的RadioButton使用小技巧
来源:互联网 发布:淘宝小商品推广免单 编辑:程序博客网 时间:2024/05/21 15:45
当多个RadioButton同在一个容器里面的时候,多半的操作都是要得到其中一个的值这个时候我们就没有必要去为每一个RadioButton写一个CheckedChange事件,这样会写很多代码,太累了。这个时候我们就可以借住委托来添加一个新的事件,用新的事件代替所有RadioButton的CheckedChange事件。我要实现的要求就是:当选择中任意一个RadioButton的时候Label17就变成我选择的RadioButton的Text值
新事件代码如下:
//RadioButton新事件 public void radioBtn_CheckedChange(object sender, EventArgs e) { if (!((RadioButton)sender).Checked) { return; } string rechargeMoney = string.Empty; switch (((RadioButton)sender).Text.ToString()) { case "10": rechargeMoney = "10"; this.lbl_money_tip.Text = rechargeMoney; break; case "20": rechargeMoney = "20"; this.lbl_money_tip.Text = rechargeMoney; break; case "30": rechargeMoney = "30"; this.lbl_money_tip.Text = rechargeMoney; break; case "40": rechargeMoney = "40"; this.lbl_money_tip.Text = rechargeMoney; break; case "50": rechargeMoney = "50"; this.lbl_money_tip.Text = rechargeMoney; break; case "100": rechargeMoney = "100"; this.lbl_money_tip.Text = rechargeMoney; break; default: break; } }
如何使用这个事件呢?有两种方法
1、在VS2008中依次选中每一个RadioButton右击--“属性”在属性中找到CheckedChange事件,为其指定为新写的事件。如下图:
2、在初始化窗体的时候添加如下代码:
public StartPage() { InitializeComponent(); this.radio_Money_10.CheckedChanged += new EventHandler(this.radioBtn_CheckedChange); this.radio_Money_20.CheckedChanged += new EventHandler(this.radioBtn_CheckedChange); this.radio_Money_30.CheckedChanged += new EventHandler(this.radioBtn_CheckedChange); this.radio_Money_40.CheckedChanged += new EventHandler(this.radioBtn_CheckedChange); this.radio_Money_50.CheckedChanged += new EventHandler(this.radioBtn_CheckedChange); this.radio_Money_100.CheckedChanged += new EventHandler(this.radioBtn_CheckedChange); }
到此这个简单的方法就完成了,让我少写了不少的垃圾代码;可以举一反三。比如复选框被选中,传出去一个值等等。这也让我对委托有了更清晰了理解。
- WinForm的RadioButton使用小技巧
- C#winform RadioButton使用技巧
- Winform 中使用小技巧
- winform的一些小技巧
- c#开发WinForm的几个小技巧
- Winform的RadioButton控件分组
- WINFORM开发小技巧
- winform透明小技巧
- Winform中WebBrowser控件的使用技巧
- Winform部分控件的使用技巧
- WinForm中的几个小技巧
- [WinForm每日一帖]TableLayoutPanel的两个小技巧
- C# WinForm清空界面控件值的小技巧
- c#的Winform开发小技巧集锦(一)
- C# WinForm清空界面控件值的小技巧
- winform 清空界面控件的小技巧
- WinForm 清空界面控件值的小技巧
- WinForm 清空界面控件值的小技巧
- perl滑动匹配核心代码
- Ibatis This SQL map does not contain a MappedStatement SelectAll 错误注意
- jsp 实现页面定时跳转
- 基于visual Studio2013解决C语言竞赛题之0601判断素数函数
- SQL的两种类型转换方式
- WinForm的RadioButton使用小技巧
- 论坛源码推荐(12月4日)
- ubuntu下Firefox浏览器的更新
- 16行Js代码的旋转地球
- Android 通过solid来定义不同边框的颜色,可以只定义一个边框的颜色
- 安装ADT的时候报错了(关于requires 'org.eclipse.gef 0.0.0' but it could not be found的解决方法)
- FLEX元标签_Inspectable
- 用extern定义全局变量
- windows下使用GUI 把本地代码提交到git远程仓库