WinForm中怎么设置用户控件的按钮事件

这篇文章给大家介绍WinForm中怎么设置用户控件的按钮事件,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

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

WinForm中怎么设置用户控件的按钮事件

 using System;   using System.Collections.Generic;   using System.ComponentModel;   using System.Drawing;   using System.Data;   using System.Linq;   using System.Text;   using System.Windows.Forms;   using System.Runtime.InteropServices;   namespace MES.Common  {      public partial class UserControlFind : UserControl      {           public UserControlFind()          {              InitializeComponent();          }           //增加 一个事件,项目里面用到这个控件的时候就可以使用这个事件了。          [EditorBrowsable(EditorBrowsableState.Always)]          [Browsable(true)]          public event EventHandler U_Click;                   public void btnFindValue_Click(object sender, EventArgs e)          {              if (U_Click != null)                  U_Click(this, e);            }     }  }
UserControlFind u = new UserControlFind();   //Find_Grid就是用户控件里面的查找方法,这里直接调用它并传入一个字符串和一个DataGridView  u.Find_Grid(userControlFind_OP.txtValue.Text.Trim(), this.dgv);

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

效果如图:

WinForm中怎么设置用户控件的按钮事件

关于WinForm中怎么设置用户控件的按钮事件就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。