winform 调用生成按钮的事件(举一反三适用其他)

 我在PANEL里生成了10个按钮,分别命名为1,2,3……10, 请问怎样在别的sub中调用panel里生成的某个按钮,比如调用名称为5的按钮的鼠标点击事件。
 

    private void Form9_Load(object sender, EventArgs e)
        {
            for (int i = 0; i <= 10; i++)
            {
                Button Lbtn = new Button();
                 Lbtn.Name = i.ToString();
                 Lbtn.Text = i.ToString();;
                Lbtn.Visible = true;
                Lbtn.Height = 32;
                Lbtn.Width = 100;
                Lbtn.Left = 32;
                Lbtn.Top = 32 * (i - 1);
                string a=Lbtn.Name+"_Click";
                //Lbtn.Click +=new EventHandler(Lbtn_Click);
                panel1.Controls.Add(Lbtn);
            }
            Yest(sender, e); 
        }

        public void Yest(object a, EventArgs b)
        {
           foreach (Control c in this.Controls)
            {
                Panel p = c as Panel;
                if (p != null)
                {
                    foreach (Control cc in p.Controls)
                    {
                        Button c_btn = cc as Button;
                        if (c_btn != null)
                        {
                            if (c_btn.Name == "5")
                            {
                               c_btn.Click+=new EventHandler(c_btn_Click);
                               // MessageBox.Show("Lbtn5");
                                break;
                            }
                        }

                    }
                }
            }
        }
        private void c_btn_Click(object sender, EventArgs e)
        {
            MessageBox.Show(sender.ToString());
        }

阅读更多
个人分类: WinForm学习
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭