zoukankan      html  css  js  c++  java
  • WinForm的RadioButton使用小技巧

    http://www.cnblogs.com/sjrhero/articles/1883155.html

    当多个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);
    }

    到此这个简单的方法就完成了,让我少写了不少的垃圾代码;可以举一反三。比如复选框被选中,传出去一个值等等。这也让我对委托有了更清晰了理解

  • 相关阅读:
    java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä'...解决
    Dos命令查看端口占用及关闭教程
    IDEA中Tomcat启动出现乱码
    ORA-12514:TNS:监听程序当前无法识别连接描述符中请求的服务
    MySQL 面试问题分析总结
    深入Cpython (编写一个Cpython 模块)
    使用docker构建简约高效的镜像
    深入理解C
    ELK 起航
    jquery
  • 原文地址:https://www.cnblogs.com/Echo529/p/6382374.html
Copyright © 2011-2022 走看看