【发布时间】:2015-08-07 11:21:38
【问题描述】:
简短版:
有没有一种方法可以在 DataGridView 控件上添加属性并覆盖 OnMouseDown / OnMouseUp 功能,而无需创建我自己的扩展 DataGridView 的控件?
长版(附解释):
我正在现有应用程序的网格之间实现多行的拖放移动。我有一个具有所需功能的扩展 DataGridView 控件,它可以完美地在此网格的实例之间移动行。
这是扩展的 DGV 类代码:
public partial class DragDropGrid : DataGridView
{
/// <summary>
/// When set, the mouse down event and click events don't happen until the mouse button is released.
/// </summary>
public bool DelayMouseDown = false;
public int MouseDownRowIndex = -1;
protected override void OnMouseDown(MouseEventArgs e)
{
if (DelayMouseDown)
{
return;
}
base.OnMouseDown(e);
}
protected override void OnMouseUp(MouseEventArgs e)
{
if (DelayMouseDown)
{
base.OnMouseDown(e);
}
base.OnMouseUp(e);
}
}
但是,我还有另一个网格(自定义 UserControl),我需要能够处理相同的拖放功能。这个网格是来自我们的基类套件的复合用户控件,并且有一个 DataGridView 以及控件上的一堆其他内容。
我尝试了以下方法来实现与我已成功扩展的基本 DataGridView 相同的功能,但没有成功:
将我的基类网格扩展为与 DataGridView 相同。这 不能像覆盖 MouseDown / MouseUp 方法那样工作 自定义 UserControl,而不是位于控件上的 DataGridView - 所以 单击网格不会触发覆盖的方法。
使用属性/重写方法更新基类网格。 这和上面有同样的问题; MouseDown / MouseUp 是 覆盖 UserControl 的方法,因此在
上单击 DataGridView 控件不会命中被覆盖的方法。
我希望只添加属性并覆盖整个 DataGridView 类的方法,以便自定义 UserControl 上的 DGV 可以与其他 DataGridView 控件一起使用该功能。
救命!
【问题讨论】:
-
鼠标操作不能直接添加handler吗?如果你的 DataGridView 被称为 dgv,那么 dgv.OnCellMouseDown += MyHandlerMethodName?
-
我的问题可能不够清楚,但我不想订阅 MouseDown / MouseUp 事件,我想覆盖网格的 OnMouseDown / OnMouseUp 函数。我将在问题的扩展代码中进行编辑,以便您了解我的意思。
-
抱歉,我误会了。粗略一看,Luaan 的回答似乎是一个不错的方法。
标签: c# winforms datagridview