winform中 radioButton遍历方法

众所周知,在winform中使用radioButton时无法像在web中使用radio那样方便,在winform中如果需要判断radio的值时,通常会先遍历所有的radio,取其选中项的值。而在web中,只需要读取radio中name相同的值。

    在winform中,可以通过遍历的方式来读取radio的选中值,方法如下:

 

Code
private void button1_Click_1(object sender, EventArgs e)
        {
            
string str = " ";
            
for (int i = 0; i < this.groupBox1.Controls.Count; i++)
            {
                RadioButton cb 
= this.groupBox1.Controls[i] as RadioButton;
                
if (cb.Checked == true)
                {
                    str 
= cb.Text.ToString() + str;
                }
            }
            MessageBox.Show(str);
        }

 

另外,还有一种思路,就是给所有的RadioButton添加一个CheckedChanged事件,然后每个事件的commondname不一样,这样就可以通知commondname来作出不同的判断。

posted @ 2009-03-02 09:53  菜鸟老师  阅读(4825)  评论(0编辑  收藏  举报