封装控件事件

实现个小功能

需求:点击textBox1 控件 需要显示位置,生成控件后还需要实现点击事件

1创建封装事件


        public event Action<object, MouseEventArgs> MouseClick_ex;

2创建事件实现功能,调用新事件

        private void textBox1_MouseClick(object sender, MouseEventArgs e)
        {
            textBox2.Text = e.X + "," + e.Y;
            MouseClick_ex(sender, e);
        }

3创建事件并调用

   UserControl1 uc = new UserControl1();
        public 测试控件封装事件()
        { 
            uc.Parent = this;
            InitializeComponent();
            uc.MouseClick_ex += Uc_MouseClick_ex;
        }

        private void Uc_MouseClick_ex(object arg1, MouseEventArgs arg2)
        {
            MessageBox.Show(arg2.X+","+ arg2.Y);
        }

猜你喜欢

转载自blog.csdn.net/neo9781467/article/details/85683807