WinForm 用户控件使用——设置用户控件的按钮事件

项目中需要对一个DataGridView控件进行类似于Excel查找的功能,之前是使用的DevExpress里面的DataGrid,用起来倒是很方便,它的列头可以和Excel一样进行随意的筛选,但是那个是收费的东东,我用了几天破解版的,担心以后会有影响所以还是决定换掉它,VS自带的DataGridView跟DevExpress里面的DataGrid相比确实相差太远了,样式不好看不说,功能上也欠缺了很多,为了满足用户的需求只得做一个查找定位的功能出来勉强满足一下用户的需求,

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Drawing;
 5 using System.Data;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Windows.Forms;
 9 using System.Runtime.InteropServices;
10 
11 namespace MES.Common
12 {
13     public partial class UserControlFind : UserControl
14     { 
15         public UserControlFind()
16         {
17             InitializeComponent();
18         }
19 
20         //增加 一个事件,项目里面用到这个控件的时候就可以使用这个事件了。
21         [EditorBrowsable(EditorBrowsableState.Always)]
22         [Browsable(true)]
23         public event EventHandler U_Click;        
24 
25         public void btnFindValue_Click(object sender, EventArgs e)
26         {
27             if (U_Click != null)
28                 U_Click(this, e);  
29         }
30    }
31 }
1 UserControlFind u = new UserControlFind();
2 
3 //Find_Grid就是用户控件里面的查找方法,这里直接调用它并传入一个字符串和一个DataGridView
4 u.Find_Grid(userControlFind_OP.txtValue.Text.Trim(), this.dgv);

如果需要操作用户控件里面其它控件,那么该控件的Modifiers属性需要设置为Public才可以。

效果如图:

作者:Allen Chen无影
邮箱:allen0717@163.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
【推广】 免费学中医,健康全家人
原文地址:https://www.cnblogs.com/allen0118/p/2498895.html