如题:
我想把字符串"RadioButton1"强转成RadioButton按钮
比如
1.  RadioButton aaa = new RadioButton()
2.  aaa = (ControlIDConverter)("RadioButton1");
3.  aaa .Checked = true;当然,第2步是错的...请问正确第2步咋写?...
在线=答案

解决方案 »

  1.   

                this.radioButton1.AutoSize = true;
                this.radioButton1.Location = new System.Drawing.Point(82, 112);
                this.radioButton1.Name = "radioButton1";
                this.radioButton1.Size = new System.Drawing.Size(95, 16);
                this.radioButton1.TabIndex = 0;
                this.radioButton1.TabStop = true;
                this.radioButton1.Text = "radioButton1";
                this.radioButton1.UseVisualStyleBackColor = true;
      

  2.   

    RadioButton RadioButton1= new RadioButton()RadioButton1.Checked = true; 是这样吗?
      

  3.   

                RadioButton t = new RadioButton();
                t.Text = "RadioButton";
                panel1.Controls.Add(t); 
      

  4.   

    可以用反射来做。但必须删除了你的那个数字1   Assembly _Assembly =Assembly.Load(" System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
                Type _ControlType=_Assembly.GetType("System.Windows.Forms."+"RadioButton");            RadioButton _Control = (RadioButton)Activator.CreateInstance(_ControlType);如果你不知道 Assembly 那来的 尝试反射所有的程序集。参考
    http://blog.csdn.net/zgke/archive/2009/03/11/3980137.aspx
      

  5.   

       Assembly _Assembly =Assembly.Load(" System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
                Type _ControlType=_Assembly.GetType("System.Windows.Forms."+"RadioButton");            RadioButton _Control = (RadioButton)Activator.CreateInstance(_ControlType);
      

  6.   

    问题详细是这样的
    我现在有5个RadioButton 分别叫RadioButton1,RadioButton2,RadioButton3,RadioButton4,RadioButton5
    我可以从数据库里获取到用户选的值是几
    比如从数据库里得到值"2"
    于是我就想要RadioButton2.Checked = true; 
    当然,我可以写成
    switch (数据库里的值)
    {
        case 1:
            RadioButton1.Checked = true; 
            break;
        case 2:
            RadioButton2.Checked = true; 
            break;
        case 3:
            RadioButton3.Checked = true; 
            break;
        case 4:
            RadioButton4.Checked = true; 
            break;
        case 5:
            RadioButton5.Checked = true; 
            break;
    }
    但是由于RadioButton数量过多(30多个,不能写那么冗长的switch)
    所以我只能这样写
    string RadioButtonName = "RadioButton" + "数据库里的值";
    RadioButton aaa = (ControlIDConverter)(RadioButtonName); 
    aaa.Checked = true; 
    大家理解了我的问题了吗?
                            
      

  7.   

    string stra = "RadioButton1" ;
           
            RadioButton TextBoxa = (RadioButton)this.FindControl(stra); 
      

  8.   

    使用控件数组(要手动添加)或者给RADIOBUTTON里面的TAG加值
    比如
    RADIOBUTTON1.TAG=1;
    RADIOBUTTON2.TAG=2;
    如果数据库中取到num
    foreach (Control ctl in this.Controls)
    {
      if (ctl.Tag.ToString() == num.ToString())
      {
        ((RadioButton)ctl).Checked=True;
      }
    }