I have a datagridview, which show a notify error icon for each cell in edit mode where user enters an invalid value. This icon is placed at the rightmost of the cell. When user move mouse over the notify error icon, I check if mouse position is within the area that contains the notify error icon. If so I show a tooltip with cell's errorText message. Below the code for on move mouse on the datagridview. Below object currentEditCellToolTip is a global ToolTip object and currentEditCellRectangle is the area that contains the nofity error icon at the rightmost of the cell.
private void GridViewMouseMove(object sender, MouseEventArgs e)
{
// MouseHover event is too slow to update coordinates so used MouseMove
// to get immediatelly response.
Point mousePosition = new Point();
DataGridView.HitTestInfo hitTestInfo;
try
{
mousePosition = datagridview.PointToClient(MousePosition);
}
catch (Exception)
{
return;
}
hitTestInfo = this.datagridview.HitTest(mousePosition.X, mousePosition.Y);
DataGridViewCell cell = this.datagridview.CurrentCell;
if (hitTestInfo.Type == DataGridViewHitTestType.Cell &&
hitTestInfo.ColumnIndex == cell.ColumnIndex &&
hitTestInfo.RowIndex == cell.RowIndex &&
this.currentEditCellRectangle.Contains(e.X - 82, e.Y - 5))
{
currentEditCellToolTip.Show(cell.ErrorText,
datagridview.EditingControl, 3000);
}
}
The problem is that I get an error: "cannot access to a disposed object". I have googled and some people says there is an internal bug inside tooltip that I refers to the previous window handle and not to the current one. So I have tried to create a new instance of ToolTip each time mouse is over the notify icon and then show it rather than always using the same tooltip with different error text and window handle. Anyway, it continues saying "cannot access to a disposed object".
currentEditCellToolTip = new ToolTip();
currentEditCellToolTip.Show(cell.ErrorText,
datagridview.EditingControl, 3000);
This ToolTip is shown when user move mouse over the area where notify icon is for the cell that is currently in edit mode and in error.
So how to make tooltip to work correctly without raising any exception like commented?
EDIT - DESCRIPTION IN DEPTH: A little example: User is editing a cell and he enters an invalid value so I have make that cell remains in edit mode until a valid value is entered and a notify error icon by the cell is shown at its rightmost. The area (rectangle) that contains this notify error icon shows an errorText describing the error when user move mouse over this area. This message is shown through a tooltip.
Imagine now, that user now enters a valid value for the current cell and change to another new cell in which he enters in edit mode and he enters an invalid value so notify error icon is shown at its rightmost for this cell and a new tooltip with the error message is shown when user move mouse over this area. And so on.... validates the current cell with a valid value and change to another new cell...
The problem here is that previous tooltip that was associated with the previous cell "EditingControl" has gone by the time the code executes (internally tooltip has a local variable, associated to the old window) and this cause the exception to be raised. EditingControl has changed and associated with the new tooltip, but the old tooltip continues associated with the old EditingControl that has already gone so this cause the exception. This is a problem related to tooltip, see below link:
ToolTip: Cannot access to a disposed object
So Is there a way to tell tooltip previous editingControl has already gone and then associated correctly the new editingControl to a new tooltip without causing any exception? Do I need to destroy the previous ToolTip in some way? or is it not necessary?