label1
label2
label3
...
labeln如何用名字去控制控件了?是如何写表达式的,,求教

解决方案 »

  1.   

    没太明白你的意思...
    什么叫 控制控件??
    如果你想得到对应名字的控件  可以从它的容器里得到
    容器.Controls["label1"]   得到就是label1控件
      

  2.   

    就是说我能不能用变量(i)来对labeli进行操作?
      

  3.   

    可以,利用反射看看Type类就会了
      

  4.   

    TO gyhanonline(猫) 大大,能讲的详细点吗?或者给点资料或者代码学习下,,
      

  5.   

    using System;
    using System.Reflection;public class MyClass
    {
        public int i = 10;
        public string k = "My Field";
        public int MyMethod(int i)
        {
            this.i = i;
            return i;
        }
        public string MyMethod(string k)
        {
            this.k = k;
            return k;
        }
    }
    public class Type_GetMethod
    {
        public static void Main()
        {
            try
            {
                // Get the type of MyClass.
                Type myType = typeof(MyClass);
                // Get the attributes and metadata of MyMethod.
                MethodInfo myMethodInfo = myType.GetMethod("MyMethod", 
                    BindingFlags.Public | BindingFlags.Instance, null, 
                    new Type[] {typeof(int)}, null);
                Console.WriteLine("\n Declaring type of the method {0} is: \n {1}", myMethodInfo,
                    myMethodInfo.DeclaringType);
                // Get the attributes and metadata of MyMethod.
                MethodInfo myMethodInfo1 = myType.GetMethod("MyMethod", 
                    BindingFlags.Public | BindingFlags.Instance, null, 
                    new Type[] {typeof(string)}, null);
                Console.WriteLine("\n Declaring type of the method {0} is: \n {1}", myMethodInfo1,
                    myMethodInfo1.DeclaringType);
            }
            catch(Exception e)
            {
                Console.WriteLine("Exception: {0}", e.Message);
            }
        }
    }
      

  6.   

    aiya这个说的对
    Controls["label1"] 得到就是label1控件
    不用那么麻烦
      

  7.   

    你拼一下你的labelname就可以了吧。 不一定要反射
    for(int i = 1; i < n ;i ++){
    labelName= string.Contact("label",i);
    容器.Controls[labelName] 就是你的label了 
    }当然反射也是可以的
      

  8.   

    我理解错了,我以为你知知道类的名称。
    你知道了对象明就没问题了,不用反射。
    Controls["label1"]这就已经到label1这个对象了
      

  9.   

    恩,上面的办法不错,比循环一遍controls好