你需要用反射
这个方法可以做到据字符串获得成员
http://blog.csdn.net/alias88/archive/2004/09/22/113058.aspx如:
object obj=  GetMember(frmMain,"txtName",null)       //取得frmMain的txtName控件
object obj=  GetMember(frmMain,"txtName.Text",null)  //取得frmMain的 txtName.Text值你只要稍微改一下这个方法,将 BindingFlags.GetField | BindingFlags.GetProperty 改为
 BindingFlags.SetField | BindingFlags.SetProperty ,再在方法参数中传入一个值,就可应用到
给字符串表示的控件设置值

解决方案 »

  1.   

    用obj.GetType().InvokeMember,还可以执行字符串表示的方法public static void ExcuteMethod(string MethodName ,object[] args , object obj  )
    {
    System.Type[] pTypes;
    if (args.Length >0)
    {
        pTypes=new System.Type[args.Length ];
                 for (i=0 ;i<args.Length ;i++)
        {
                   pTypes[i]=args[i].GetType() ;
                 }
    }
    else
    pTypes=new System.Type[0];         System.Reflection.MethodInfo mMethod ;
             Type objType=obj.GetType();
             mMethod = objType.GetMethod (MethodName,BindingFlags.Public | 
                            BindingFlags .Static | BindingFlags .NonPublic  |  
                            BindingFlags .Instance,null,pTypes,null );
             if (mMethod !=null)
             {
                if (mMethod.IsStatic)
            mMethod.Invoke(null, args);
             else
                    mMethod.Invoke(obj, args);
             }
    .......
      

  2.   

    请教alias88() :
    用了您http://blog.csdn.net/alias88/archive/2004/09/22/113058.aspx里面的代码。没有改动。想这样。
    object obj=  GetMember(frmMain,"btnXTJS",null);      //“btnXTJS”是下拉菜单中按钮的名称。
    (obj as Control).Enabled=false; 可报错。说:
    frmMain表示“类”,此处应为“变量”什么原因呢?
      

  3.   

    frmMain表示“类”,此处应为“变量”
    =========================================frmMain 好像是类的名字吧 应该是这个类的实例
      

  4.   

    frmMain newfrmMain = new frmMain();
    object obj=  GetMember(newfrmMain,"btnXTJS",null);
    (obj as Control).Enabled=false; 这样改编译通过了。可是出现了无限循环。
      

  5.   

    使用反射
    使用反射生成一个窗体:
    private void button1_Click(object sender, System.EventArgs e)
    {
    Assembly assm = Assembly.LoadFrom("e:\\WindowsApplication5.dll");
    Type TypeToLoad= assm.GetType("WindowsApplication5.Form1");

    object obj;
    obj = Activator.CreateInstance(TypeToLoad);
    Form formToShow = null;
    formToShow = (Form)obj;
    formToShow.Show();

    }
      

  6.   

    for (int i=0; i<strMember.Length && strMember[i]!="" ;i++)
    中间的strMember[i]!="" 去掉改为
    for (int i=0; i<strMember.Length ;i++)
    {
       if( strMember[i].Equels("") ) continue;
       ....}