Reputation: 222
I am working on a form that uses a SharpMap MapBox to display objects as points on a world map. Currently, if I enter the MapBox (mapBox1) with the cursor and stop on the point, it displays the tooltip as I want it to. However, once I've stopped the mouse within the MapBox (not necessarily on the point) and move the mouse within the MapBox, moving to the point will not (re)display the tooltip. However, if I leave the MapBox (say, moving the cursor out of the window or onto one of the menu strips, or onto a button overlaid on the map) I can then get the tooltip to appear, but only once before I have to move the cursor as before.
What is causing this behaviour, and is there any easy way to fix it?
I've tried using ToolTip.Hide(), ToolTip.Active = false (and then setting it to true again when I want it displayed) and refreshing the MapBox at various points.
Relevant code:
The ToolTip is global, and the constructor defines it as follows:
toolTip.InitialDelay = 1000;
toolTip.ReshowDelay = 750;
toolTip.ShowAlways = true;
I then have two event handlers for the mouse, both tied to the MapBox. "obj" is a global object of a custom class containing the latitude and longitude points.
private void mapBox1_MouseHover(object sender, EventArgs e)
{
PointF pos = mapBox1.PointToClient(Cursor.Position);
int screenToleranceX = 20, screenToleranceY = 20;
PointF posLow = new PointF(pos.X - screenToleranceX, pos.Y - screenToleranceY);
PointF posHigh = new PointF(pos.X + screenToleranceX, pos.Y + screenToleranceY);
GeoAPI.Geometries.Coordinate objLoc = new GeoAPI.Geometries.Coordinate(obj.longitude, obj.latitude);
PointF objPoint = mapBox1.Map.WorldToImage(objLoc);
if (posLow.X <= objPoint.X && objPoint.X <= posHigh.X && posLow.Y <= objPoint.Y && objPoint.Y <= posHigh.Y)
{
toolTip.Active = true;
toolTip.Show(obj.Name, mapBox1, mapBox1.PointToClient(Cursor.Position));
}
}
private void mapBox1_MouseMove(GeoAPI.Geometries.Coordinate worldPos, MouseEventArgs imagePos)
{
PointF pos = mapBox1.PointToClient(Cursor.Position);
int screenToleranceX = 20, screenToleranceY = 20;
PointF posLow = new PointF(pos.X - screenToleranceX, pos.Y - screenToleranceY);
PointF posHigh = new PointF(pos.X + screenToleranceX, pos.Y + screenToleranceY);
GeoAPI.Geometries.Coordinate objLoc = new GeoAPI.Geometries.Coordinate(obj.longitude, obj.latitude);
PointF objPoint = mapBox1.Map.WorldToImage(objLoc);
if (toolTip.Active && (posLow.X > objPoint.X || objPoint.X > posHigh.X || posLow.Y > objPoint.Y || objPoint.Y > posHigh.Y))
{
toolTip.Active = false;
}
}
** EDIT **
As per the accepted answer, I have the following code as a solution, with hopes to refine it further as needed. This works for now, however (using an externally declared bool, toolTipDisp, defaulted to false):
private void mapBox1_MouseMove(GeoAPI.Geometries.Coordinate worldPos, MouseEventArgs imagePos)
{
PointF pos = mapBox1.PointToClient(Cursor.Position);
int screenToleranceX = 20, screenToleranceY = 20;
PointF posLow = new PointF(pos.X - screenToleranceX, pos.Y - screenToleranceY);
PointF posHigh = new PointF(pos.X + screenToleranceX, pos.Y + screenToleranceY);
GeoAPI.Geometries.Coordinate objLoc = new GeoAPI.Geometries.Coordinate(obj.longitude, obj.latitude);
PointF objPoint = mapBox1.Map.WorldToImage(objLoc);
if (posLow.X <= objPoint.X && objPoint.X <= posHigh.X && posLow.Y <= objPoint.Y && objPoint.Y <= posHigh.Y)
{
if (!toolTipDisp)
{
toolTip.Show(obj.Name, mapBox1, mapBox1.PointToClient(Cursor.Position));
toolTipDisp = true;
}
}
else
{
toolTip.Hide(mapBox1);
toolTipDisp = false;
}
}
Upvotes: 1
Views: 1693
Reputation: 21999
Try this (pseudo-code):
private string _previous;
private void mapBox1_MouseMove(GeoAPI.Geometries.Coordinate worldPos, MouseEventArgs imagePos)
{
var text = ...; // generate tooltip text based on the new position
if(text != _previous)
{
_previous = text;
tooltip.Show(text, mapBox1, mapBox1.PointToClient(imagePos.Location));
}
}
private void mapBox1_MouseLeave(object sender, EventArgs e)
{
toolTip.Hide(mapBox1);
}
Upvotes: 1