在当前窗体中有button1和button2两个按钮,当点击button1的时候会在窗体中创建对象Label1并设置其Text="标签",点Button2,检测窗体中是否存在Label1,如果存在则创建一个指向该对象的实例并最终用消息框显示Label1.Text。
请问此功能应该如何实现?

解决方案 »

  1.   


    private Label label = null;Onclick_button1(object sender, EventArgs e)
    {
        label = new Label();
    }Onclick_button2(object sender, EventArgs e)
    {
        if(label != null)
        {
             MessageBox.Show(label.Text);
        }
    }
      

  2.   


    private void button1_Click(object sender, EventArgs e)
    {
        Label label = new Label();
        label.Name = "Label1";
        label.Text = "标签";
        this.Controls.Add(label);
    }private void button2_Click(object sender, EventArgs e)
    {
        Control[] controls = this.Controls.Find("Label1", false);
        if (controls.Length == 1)
        {
            MessageBox.Show(controls[0].Text);
        }
    }
      

  3.   

    反射的方法,在这个例子中没有任何实用性
    private void button1_Click(object sender, EventArgs e)
    {
        Label label = new Label();
        label.Name = "Label1";
        label.Text = "标签";
        this.Controls.Add(label);
    }private void button2_Click(object sender, EventArgs e)
    {
        PropertyInfo info = this.GetType().GetProperty("Controls");
        ControlCollection collection = (ControlCollection)info.GetValue(this, null);
        Control[] controls = collection.Find("Label1", false);
        if (controls.Length == 1)
        {
            MessageBox.Show(controls[0].Text);
        }
    }
      

  4.   

    谢谢两位的回答,可能是我描述的不够清楚。using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {        }        private void button1_Click(object sender, EventArgs e)
            {
                myclass.YourSelect(textBox1.Text);
            }
        }    public class myclass
        {
            public static void YourSelect(string No)
            {
                if (No == "1")
                {
                  Label label=????;//此处判断是否存在label1并获取句柄
                    MessageBox.Show(label.Text);
                }
                else
                { 
                  Label label=????;//此处判断是否存在label2并获取句柄
                    MessageBox.Show(label.Text);
                }
            }
        }
    }也就是说要调用另一个类,这样不知道能不能实现?
      

  5.   

    我试了一下,使用四楼的方法是可以的,但还是必须将当前的Form传递过去,有没有办法不用传递Form而达到目的?
    使用4楼方法实现代码如下:
    namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {        }        private void button1_Click(object sender, EventArgs e)
            {
                myclass my = new myclass();
                my.YourSelect(this,textBox1.Text);
            }
        }    public class myclass
        {
            public void YourSelect(System.Windows.Forms.Form form, string No)
            {
                PropertyInfo info = form.GetType().GetProperty("Controls");
                System.Windows.Forms.Control.ControlCollection collection = (System.Windows.Forms.Control.ControlCollection)info.GetValue(form, null);
                Control[] controls = collection.Find("label" + No, false);
                if (controls.Length == 1)
                {
                    MessageBox.Show(controls[0].Text);
                }
            }
        }
    }
      

  6.   

    我实际遇到的情况是处理资源文件,在Form调用一个处理资源文件的类,如果传递的参数是KR则载入Resources_ko_KR.resx,否则载入Resources_zh_CN.resx;由于资源文件并不能被包含在Form中,因此不能通过我楼上的方法解决问题。
    因相关代码非常的长,因此不便贴出。
    希望没把大家整糊涂,抱歉抱歉,如果问题解决我再加分。
      

  7.   

    使用3楼的方法就行了,4楼的方法绕一大圈效率不高。
    不传form是绝对不可能的。