DataGridView CellMouseEnter事件未按预期工作 [英] DataGridView CellMouseEnter Event not working as expected
问题描述
大家好,
我扩展了DataGridView
,使其具有比默认控件更多的功能.我添加了一项功能,如果单元格无法显示其所有内容,该功能将显示带有图标的PictureBox
.如果单击该图标,它将更改所属行的换行模式,并将AutoSizeRowsMode
更改为displayed-cells.
我通过听CellMouseEnter
事件显示该图标,然后计算矩形以将该图标放置在单元格的右下角,如下所示:
Hi all,
I have extended the DataGridView
to have more features than the default control. I have added a feature that shows a PictureBox
with an icon if a cell cannot display all it''s contents. If the icon is clicked it changes the wrap-mode of the owning row and changes the AutoSizeRowsMode
to displayed-cells.
I''m showing this icon by listening to the CellMouseEnter
event and then calculating the rectangle to place the icon in the bottom right corner of the cell as follows:
if ((e.RowIndex < 0) || (e.ColumnIndex < 0))
{
//_peExpand is a normal PictureBox
_pbExpand.Visible = false;
return;
}
DataGridViewCell CellToUse = this[e.ColumnIndex, e.RowIndex];
if (CellToUse.Size.Width < CellToUse.PreferredSize.Width)
{
Rectangle DisplayRect = this.GetCellDisplayRectangle(e.ColumnIndex,
e.RowIndex, false);
_pbExpand.Location = new Point(DisplayRect.Right -
_pbExpand.Image.Width, DisplayRect.Bottom - _pbExpand.Image.Height);
_pbExpand.Tag = CellToUse;
_pbExpand.Visible = true;
_pbExpand.Image = (CellToUse.OwningRow.DefaultCellStyle.WrapMode ==
DataGridViewTriState.True) ? Resources.chevron_icon :
Resources.chevron_expand_icon;
}
我的问题出在行已扩展并且鼠标指针离开单元格并稍后再次输入之后.它忽略PictureBox
(它仍然被绘制到正确的位置).如果将鼠标指针悬停在PictureBox
上,则它将触发CellMouseEnter
事件数百次,而不是触发PictureBox
的MouseEnter
事件.如果行被扩展并且指针从不离开单元格,则它可以完美地工作.如果有人能指出正确的方向,我将不胜感激.
编辑 :仅当DataGridView
属性AutoSizeRowsMode
设置为DisplayedCells时,才会发生这种情况.似乎也只有在单元&上存在控件时,才会发生这种情况.鼠标指针进入该控件.
My problem comes after the row has been expanded and the mouse pointer leaves the cell and enters again later. It ignores the PictureBox
(it''s still drawn to the correct place). If I hold the mouse pointer over the PictureBox
then it fires the CellMouseEnter
event hundreds of times instead of firing the MouseEnter
event for the PictureBox
. It works perfectly fine if the row is expanded and the pointer never leaves the cell. If anyone can point me in the right direction I would appreciate it.
Edit: It only seems to happen when the DataGridView
property AutoSizeRowsMode
is set to DisplayedCells. It also seems to happen only when there is a control on the cell & the mouse pointer enters that control.
推荐答案
找到了!!
因为我在CellMouseEnter
事件中将PictureBox
设置为visible = true,所以我需要在CellMouseLeave
事件中将其设置为false.该问题是由于将PictureBox
设置为visible = false引起的.我评论了一下,它解决了问题.
Found it!!
Because I''m setting thePictureBox
to visible = true in theCellMouseEnter
event I needed to set it to false in theCellMouseLeave
event. The problem is caused by setting thePictureBox
to visible = false. I commented out that bit and it resolved the issue.
这篇关于DataGridView CellMouseEnter事件未按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!