CellMouseEnter事件未按预期工作

CellMouseEnter事件未按预期工作

本文介绍了DataGridView CellMouseEnter事件未按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我扩展了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事件数百次,而不是触发PictureBoxMouseEnter 事件.如果行被扩展并且指针从不离开单元格,则它可以完美地工作.如果有人能指出正确的方向,我将不胜感激.

编辑 :仅当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.

推荐答案


这篇关于DataGridView CellMouseEnter事件未按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 13:58