public class Form1 : Form
{
    public int Count;    //某个方法里
    protected void Method()
    {
        Class1 c = new Class1();
        c.AddLabel(this,out Count);
    }
}
public class Class1
{
    public void AddLabel(Form f,out int count)
    {
        count++;
        Label l = new Label();
        l.Name = "label" + count;
        l.Text = "****";
        //继续为l其他属性赋值
        f.Controls.Add(l);
    }
}
当我双击 lable的时候打开FROM2 这个事件怎么写?

解决方案 »

  1.   

    需要在 CLASS1中添加事件 
      

  2.   

        public class Class1
        {
            public void AddLabel(Form f, out int count)
            {
                count++;
                Label l = new Label();
                l.Name = "label" + count;
                l.Text = "****";
                l.MouseDoubleClick += new MouseEventHandler(l_MouseDoubleClick);
                //继续为l其他属性赋值 
                f.Controls.Add(l);
            }        void l_MouseDoubleClick(object sender, MouseEventArgs e)
            {
                Form1 form = new Form1();
                form.Show();
            }
        } 
      

  3.   

    l.MouseDoubleClick += new MouseEventHandler(l_MouseDoubleClick);
      

  4.   

        class Class1
        {
            public void AddLabel(Form f, ref int count)
            {
                count++;
                Label l = new Label();
                l.Name = "label" + count;
                l.Text = "****";
                //继续为l其他属性赋值
                f.Controls.Add(l);
                l.DoubleClick += new EventHandler(l_DoubleClick);
            }        private void l_DoubleClick(object sender, EventArgs e)
            {
                Form2 o = new Form2();
                o.Show();
            } 
        }
      

  5.   

    可以在l_MouseDoubleClick中通过e参数判断是左键双击还是右键双击;然后再决定是否显示form