界面上三个radioButton控件,在程序中我想根据哪个radioButton被点中后执行相应方法,if语句实现如下                if (radioButton1.Checked)
                {
                    //方法
                }
                else if (radioButton2.Checked)
                {
                    //
                }
                else if (radioButton3.Checked)
                {
                     //
                }
但是我想改成swith case语句 应该怎么改写???

解决方案 »

  1.   

    这没办法改成switch语句。不过,你可以为这些Check定义同一个Checked事件,然后在事件里通过sender来操作他们。
      

  2.   

    定义一个变量整型,
    int i = 0;
     把这几个check放到一个group中,获取其中的值赋予i,然后用switch来处理i的不同值的情况
      

  3.   

    加一个全局变量string radText="";
    radText=radioButton1.Text;
    radText=radioButton2.Text;
    radText=radioButton3.Text;
    swith(radText)
    {
        case "":
        //
        break;
        case "":
        //
        break;
        case "":
        //
        brea;
    }
      

  4.   


    <asp:RadioButtonList ID="radl" runat="server">
        <asp:ListItem Value="1" Selected="True">选择1</asp:ListItem>
        <asp:ListItem Value="2">选择2</asp:ListItem>
        <asp:ListItem Value="3">选择3</asp:ListItem>
        </asp:RadioButtonList>
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />protected void Button1_Click(object sender, EventArgs e)
        {
            int selectValue = Convert.ToInt32(radl.SelectedValue);
            switch (selectValue)
            { 
                case 1:
                    Response.Write(selectValue);
                    break;
                case 2:
                    Response.Write(selectValue);
                    break;
                case 3:
                    Response.Write(selectValue);
                    break;
            }
        }
      

  5.   

    protected void Button1_Click(object sender, EventArgs e)
        {
            int selectValue = Convert.ToInt32(radl.SelectedValue);
            switch (selectValue)
            { 
                case 1:
                    Response.Write(selectValue);
                    break;
                case 2:
                    Response.Write(selectValue);
                    break;
                case 3:
                    Response.Write(selectValue);
                    break;
            }
        }
      

  6.   

    private void checkBox_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox checkbox = sender as CheckBox;
        if (checkbox != null)
        {
            bool check = checkbox.Checked;
            if (check)
            {
                switch (checkbox.Name)
                { 
                    case "checkbox1":
                        break;
                    case "checkbox2":
                        break;
                    case "checkbox3":
                        break;
                    default:
                        break;
                }
            }
        }
    }
      

  7.   

    额 给radioButton的tag属性定义你想要的值 比如1,2,3比如rdo1 rdo2 rdo3(winform)
    如果你讲的是web项目,楼上已经给出建议了
      

  8.   


        RadioButton curBtn = (RadioButton)sender;
        if(curBtn.checked){
           switch(curBtn) {
                case radioButton1:
                //do
                break;
                case radioButton2:
                //do
                break;
                case radioButton3:
                //do
                break;
                default:
                break;
           }
        }
      

  9.   

    改用radiobuttonlist呀,然后switch case每一个value值,就可以实现了么
      

  10.   

    string str = radioButton1.Checked == true ? radioButton1.Name : (radioButton2.Checked == true ? radioButton2.Name : radioButton3.Name);
                switch (str)
                {
                    case "radioButton1":
                        MessageBox.Show("radioButton1");
                        break;
                    case "radioButton2":
                        MessageBox.Show("radioButton2");
                        break;
                    case "radioButton3":
                        MessageBox.Show("radioButton3");
                        break;
                    default:
                        break;
                }
      

  11.   

    楼主,为什么不用枚举呢?感觉枚举可能会好点:1. 定义 enum RadioType { R1, R2, R3 },随便写写,打个比方。
    2. 在每次 radio 选中的时候触发 CheckChanged 事件,
        在这个事件里面设置全局变量的值:rdtype = .....
    3. 写 switch,怎么实现你懂的。
    switch (rdtype)
    {
        case R1: .....
        case R2: .....
        case R3: .....
    }
      

  12.   

    正解!1.在Form1_Load里注册RadioButton的CheckedChanged事件:            foreach (Control ctr in this.Controls)
                {
                    if (ctr is RadioButton)
                    {
                        ((RadioButton)ctr).CheckedChanged += radio_CheckedChanged;
                    }
                }
    2.
            private void radio_CheckedChanged(object sender, EventArgs e)
            {
                RadioButton radio = sender as RadioButton;
                if (!radio.Checked)
                {
                    return;
                }
                switch (radio.Name)
                { 
                    case "radA":
                        MessageBox.Show("a");
                        break;
                    case "radB":
                        MessageBox.Show("b");
                        break;
                    case "radC":
                        MessageBox.Show("c");
                        break;
                }
            }
      

  13.   

    if不是挺好么?确实有“switch比if要清晰”的说法,但是不能不考虑使用场景而生搬硬套,更不至于为了用switch语句去处理什么 checked changed 事件,事件不是这么用的。