屏蔽调起窗体的事件

    最近做了一个例子,假如有一个主窗体,然后要调另一个窗体,且子窗体中有DataGridView控件 ,且DataGridView本身有双击事件,然后调起的时候要用到与它不同的双击事件,这就需要我们对DataGridView本身的双击事件进行屏蔽。我们就要另写个函数来做。代码如下

private void ClearEvent(DataGridView dgvEqup)
        {

            PropertyInfo propertyInfo =
            (typeof(System.Windows.Forms.DataGridView)).GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic);

            EventHandlerList eventHandlerList = (EventHandlerList)propertyInfo.GetValue(dgvEqup, null);
            FieldInfo fieldInfo = (typeof(DataGridView)).GetField("EVENT_" + "DATAGRIDVIEW" + "CELLDOUBLECLICK", BindingFlags.Static |
            BindingFlags.NonPublic);
            Delegate d = eventHandlerList[fieldInfo.GetValue(dgvEqup)];
            if (d != null)
            {
                foreach (Delegate temp in d.GetInvocationList())
                {
                    eventHandlerList.RemoveHandler(fieldInfo.GetValue(null), temp);
                }
            }
        }


 

原文地址:https://www.cnblogs.com/ddan/p/2819542.html