How to prevent tooltip tooltip in user control?

I created a custom control, and when the condition is met, I want to show a tooltip:

protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); var plannedItem = GetPlannedItemByPosition(e.Location); if (plannedItem != null) _tooltip.SetToolTip(this, plannedItem.Description); else _tooltip.RemoveAll(); } 

This code works fine except for the person who prompts the hint.

This custom control displays all the information in the OnPaint event, maybe this has something to do with it? And if so, how can you prevent the flickering of the tooltip?

+6
source share
5 answers

This will happen when you display a tooltip at the position of the mouse cursor. As soon as the prompt window appears, Windows notices that the mouse is in this window and sends a MouseMove message. Because of this, the tooltip disappears. What makes Windows send the MouseMove message to your control using the OnMouseMove () method. Which makes the tooltip again. Etcetera, you will see a tooltip flickering quickly.

Solve this in any of the following ways:

  • Show a tooltip far from the mouse position so that it does not overlap the mouse cursor.
  • update / show tooltip when you need to change
  • set the control's Capture property to true, so the tooltip will not receive a MouseMove message
+6
source

Remember the last position of the mouse and set the tooltip only when changing the position of the mouse.

 public partial class Form1 : Form { private int lastX; private int lastY; private void button1_MouseMove(object sender, MouseEventArgs e) { if (eX != this.lastX || eY != this.lastY) { toolTip1.SetToolTip(button1, "test"); this.lastX = eX; this.lastY = eY; } } 
+6
source

Since this is a tinted user control, I think it would be easier to just have a variable that holds the last review shown, and instead of always β€œsetting” the tooltip, just show it.

A simple example (using only the form):

 public partial class Form1 : Form { private List<TipRect> _Tips = new List<TipRect>(); private TipRect _LastTip; private ToolTip _tooltip = new ToolTip(); public Form1() { InitializeComponent(); _Tips.Add(new TipRect(new Rectangle(32, 32, 32, 32), "Tip #1")); _Tips.Add(new TipRect(new Rectangle(100, 100, 32, 32), "Tip #2")); } private void Form1_Paint(object sender, PaintEventArgs e) { foreach (TipRect tr in _Tips) e.Graphics.FillRectangle(Brushes.Red, tr.Rect); } private void Form1_MouseMove(object sender, MouseEventArgs e) { TipRect checkTip = GetTip(e.Location); if (checkTip == null) { _LastTip = null; _tooltip.Hide(this); } else { if (checkTip != _LastTip) { _LastTip = checkTip; _tooltip.Show(checkTip.Text, this, e.Location.X + 10, e.Location.Y + 10, 1000); } } } private TipRect GetTip(Point p) { TipRect value = null; foreach (TipRect tr in _Tips) { if (tr.Rect.Contains(p)) value = tr; } return value; } } 

Here is the TipRect class that I created to simulate your PlannedItem class:

 public class TipRect { public Rectangle Rect; public string Text; public TipRect(Rectangle r, string text) { Rect = r; Text = text; } } 
+1
source

I assume that your mouse moves a little when you think that it is still. I suggest you do some caching here - just call _tooltip.SetToolTip if the planned Item is changed.

0
source

For visitors to this topic, here is what I did following the above suggestions (VB.NET):

 Dim LastToolTip As String Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove Dim NewToolTip = CalculateTooltipText(eX, eY) If LastToolTip <> NewToolTip Then ToolTip1.SetToolTip(PictureBox1, NewToolTip) LastToolTip = NewToolTip End If End Sub 

He stopped the flicker.

0
source

Source: https://habr.com/ru/post/905446/


All Articles