如何在父类中为子类添加事件
来源:互联网 发布:360度网络摄像头 编辑:程序博客网 时间:2024/05/22 01:28
//以下代码实现的是在父类窗体中为子类窗体中的datagrid控件添加mousedown mouseup事件。
//父类窗体中的方法
private void Form_SearchBase_Load(object sender, System.EventArgs e)
......{
Auto AddEvent MouseDown MouseUpAuto AddEvent MouseDown MouseUp#region Auto AddEvent MouseDown MouseUp
foreach ( object obj in panel2.Controls )
......{
if (obj.GetType().ToString() == "System.Windows.Forms.DataGrid")
......{
Type t = typeof(System.Windows.Forms.DataGrid);
PropertyInfo pi = t.GetProperty("Events", BindingFlags.NonPublic| BindingFlags.Instance);
EventHandlerList el = (EventHandlerList)pi.GetValue(obj,null);
FieldInfo fiA = t.GetField("MouseDown", BindingFlags.NonPublic | BindingFlags.Static);
FieldInfo fiB = t.GetField("MouseUp", BindingFlags.NonPublic | BindingFlags.Static);
if (( fiA == null) || (fiB == null))
......{
((System.Windows.Forms.DataGrid)obj).MouseDown += new MouseEventHandler(this.dataGrid_PlanCase_Mouse);
((System.Windows.Forms.DataGrid)obj).MouseUp += new MouseEventHandler(this.dataGrid_PlanCase_Mouse);
}
else
......{
Delegate dA = el[fiA.GetValue(obj)];
Delegate dB= el[fiB.GetValue(obj)] ;
if ( ( dA == null ) && ( dB == null))
......{
((System.Windows.Forms.DataGrid)obj).MouseDown += new MouseEventHandler(this.dataGrid_PlanCase_Mouse);
((System.Windows.Forms.DataGrid)obj).MouseUp += new MouseEventHandler(this.dataGrid_PlanCase_Mouse);
}
}
}
}
#endregion
}
//父类中的方法
MouseAboutMouseAbout#region MouseAbout
public void dataGrid_PlanCase_Mouse(object sender, System.Windows.Forms.MouseEventArgs e)
......{
foreach ( object obj in panel2.Controls )
......{
if (obj.GetType().ToString() == "System.Windows.Forms.DataGrid")
......{
System.Windows.Forms.DataGrid.HitTestInfo hti=((System.Windows.Forms.DataGrid)obj) .HitTest (e.X ,e.Y );
if(e.Button == System.Windows .Forms .MouseButtons .Left )
......{
if(hti.Type == System.Windows.Forms.DataGrid.HitTestType.Cell)
......{
HighlightRow(((System.Windows.Forms.DataGrid)obj), hti.Row,hti.Column );
}
}
}
}
}
HighlightRow DateGridHighlightRow DateGrid#region HighlightRow DateGrid
public void HighlightRow(System.Windows.Forms.DataGrid _Grid,int _row,int _col)
......{
int CurrentRow = _Grid.CurrentCell.RowNumber;
if (ModifierKeys == Keys.Shift)
...{
int _i=0,_j=0, _Top =0, _Bottom=0;
_i =CurrentRow;
//获得最后一次点击的行
_j = _LastSelectRow;
if ( (_i != _j ))
...{
_Top = _i;
_Bottom =_j;
GetSelectRows(_Grid);
UnSelectRows(_Grid);
}
//判断应该正序还是倒序
_i = System.Math.Min(_Top,_Bottom);
_j = System.Math.Max(_Top,_Bottom);
int i=_i ;
while (i<=_j)
...{
_Grid .Select( i );
i++;
}
_Grid .CurrentCell = new DataGridCell( _Bottom, _col );
}
else if( Control.ModifierKeys == Keys.Control)
...{
int _i=0,_j=0;
_i = Convert.ToInt32( System.Math.Min (CurrentRow,_LastSelectRow) );
_j = Convert.ToInt32( System.Math.Max(CurrentRow,_LastSelectRow) );
//将所有选择过的行全部保存
if ( _i != _j )
...{
GetSelectRows(_Grid);
UnSelectRows(_Grid);
}
foreach(object obj in _SelectRows)
...{
_Grid .Select( Convert.ToInt32( obj ) );
}
_Grid .Select( _j );
_Grid .CurrentCell = new DataGridCell( _j, _col );
}
else
...{
_Grid.UnSelect(CurrentRow);
_Grid .CurrentCell = new DataGridCell( _row, _col );
_Grid .Select ( _row );
_LastSelectRow = _row;
ClearSaveValue();
}
}
private void ClearSaveValue()
......{
_Top =0;
_Bottom=0;
_SelectRows.Clear();
}
/**//**//**//// <summary>
/// 得到所有被选中的行
/// </summary>
/// <param name="_Grid"></param>
private void GetSelectRows(System.Windows.Forms.DataGrid _Grid)
......{
System.Data.DataTable _Table;
// Assumes the DataGrid is bound to a DataTable.
if (_Grid.DataSource == null)
......{
return ;
}
else
......{
_Table = (System.Data.DataTable) _Grid.DataSource;
int RowCount = _Table.Rows.Count;
if (RowCount == 0)
......{
return ;
}
int i =0;
do
......{
if (_Grid.IsSelected(i) )
......{
_SelectRows.Add(i);
//MessageBox.Show(i.ToString());
}
i++;
}
while(i<RowCount);
}
}
//取消所有被选择的行
private void UnSelectRows(System.Windows.Forms.DataGrid _Grid)
......{
System.Data.DataTable _Table;
// Assumes the DataGrid is bound to a DataTable.
if (_Grid.DataSource == null)
......{
return ;
}
else
......{
_Table = (System.Data.DataTable) _Grid.DataSource;
int RowCount = _Table.Rows.Count;
if (RowCount == 0)
......{
return ;
}
int i =0;
do
......{
if (_Grid.IsSelected(i) )
......{
_Grid.UnSelect(i);
//MessageBox.Show(i.ToString());
}
i++;
}
while(i<RowCount);
}
}
#endregion
#endregion