你先说得明白点?你是说有一个字符串比如Textbox,你要将它转换为一个控件吗?这样的话很难

解决方案 »

  1.   

    使用
    System.Activator.CreateInstance参考
    ms-help://MS.VSCC/MS.MSDNVS.2052/cpref/html/frlrfsystemactivatorclasscreateinstancetopic.htm
      

  2.   

    变量名对一个对象来说没有任何意义但是可以通过类型名称来创建一个控件:object btn = Activator.CreateInstance(Type.GetType("System.Web.UI.WebControls.Button"));请参考这几个贴子:http://expert.csdn.net/Expert/topic/1587/1587817.xml?temp=.228512
    http://expert.csdn.net/Expert/topic/1608/1608067.xml?temp=.5653345
    http://expert.csdn.net/Expert/topic/1629/1629801.xml?temp=.2895624
      

  3.   

    一时无法理解,我现在已经创建了一个名为Text1的TextBox控件,我想把"Text1"这个字符串转换成相对应的对象
      

  4.   

    你创建的TextBox控件的ID是Text1,但是变量名称可以为Text1,也可以为别的但是在ASP.net中,由于CodeBehind要求变量名和ID一致,所以造成了你的困扰实际上你可以通过遍历Page.Contorls来获得这个对象:private TextBox GetTextBox( Control ctrl )
    {
       if( ctrl.ID == "Text1" )
          return (TextBox)ctrl;
       else if( ctrl.HasControls() )
         foreach( Control subCtrl in ctrl.Controls )
            return GetTextBox();
       else
         return null;
    }然后在你要获取的地方:TextBox tb = GetTextBox( Page );
      

  5.   

    更正一下,我现在已经创建了一个名为Text1的TextBox控件,我想把"Text1"这个字符串转换成相对应的实例(用对象似乎不大准确)。
      

  6.   

    to timmy3310(tim):
         我试了你给我的代码,可是Control类里没有ID、HasControls()这几个属性和方法啊,你看看是使用Control这个类吗?谢谢
      

  7.   

    反射呀!Type.GetType(className);
    需要使用完全限定名,也就是要加上命名空间的名称,就象:
      string className = "A";
        A obj = new A();
        obj.execute();
        Type classType = Type.GetType(className);
        //这里classType 总为null???
                 if(classType != null)
        {
    obj = (A)Activator.CreateInstance(classType);
    obj.execute();
        }
      

  8.   

    to snewxf(心疤):
         还是不大清楚,可否解释清楚一点。谢谢
      

  9.   

    Class A
    {
    ......................

        string className = "A";
        A obj ;
        Type classType = Type.GetType(className);
        //这里classType 总为null???
                 if(classType != null)
        {
    obj = (A)Activator.CreateInstance(classType);
    obj.execute();
        }
    P这样应该懂了吧!
    你在MSDN上看看Type.GetType(className);用法。当然也看看TYPE类!
      

  10.   

    呵呵!  //这里classType 总为null???这句别理它!
    这代码我是从别的贴子COPY来给你的。
    你只要看看MSDN就知道了:)好运!
      

  11.   

    哦,不过MSDN在哪里可以找到呢?(不要笑我)我的.Net安装是7张碟的。
      

  12.   

    请看看我的这段代码,问题出在哪里?
    private void Form1_Load(object sender, System.EventArgs e)
    {
    TextBox ActiveTextBox;
    foreach( Control subCtrl in Form1.ActiveForm.Controls)
    {
    if (subCtrl.Name == "textBox1")
    ActiveTextBox = (TextBox)subCtrl;
    }
    }
    调试时出现:“其他信息:未将对象引用设置到对象的实例。”
      

  13.   

    private TextBox GetTextBox( Control ctrl )
    {
       if( ctrl.Name == "Text1" )
          return (TextBox)ctrl;
       else if( ctrl.Controls.Count!=0 )
         foreach( Control subCtrl in ctrl.Controls )
            return GetTextBox();
       else
         return null;
    }然后在你要获取的地方:TextBox tb = GetTextBox( this );
      

  14.   

    不好意思,代码有点问题,改成这样:
    private TextBox GetTextBox( Control ctrl )
    {
       if( ctrl.Name == "Text1" )
          return (TextBox)ctrl;
       else if( ctrl.Controls.Count!=0 )
         foreach( Control subCtrl in ctrl.Controls )
         {
            TextBox tb = GetTextBox(subCtrl);
            if(tb!=null)
              return tb;
         }
       else
         return null;
    }然后在你要获取的地方:TextBox tb = GetTextBox( this );
      

  15.   

    打开你的VS。NET
    按F1就出来了。
    然后索引!在里面输入TYPE就可以查到TYPE类的具体描述:)
      

  16.   

    to  snewxf(心疤):
        呵呵,原来是这样,我以前是在Microsoft .NET Framework SDK的概述中打开,这种方式下索引是无法使用的(我也不知道是什么原因),奇怪。
        谢谢疤哥,呵呵
      

  17.   

    to  timmy3310(tim) :我试了你给我的代码,还是不行,怎么回事呢?