I have a TextBox
and I want that while the mouse is over the control, a will appear Button
on the right side, above the TextBox
. The fact is that the TextBox
also has a custom behavior, so both controls are embedded in a custom control that inherits from UserControl
. All of this, of course, using WinForms
.
The first thing that occurred to me is to handle the events MouseEnter
and MouseLeave
as shown below:
public partial class MiControl : UserControl
{
void textBox_MouseEnter(object sender, EventArgs e)
{
boton.Visible = true;
}
void textBox_MouseLeave(object sender, EventArgs e)
{
boton.Visible = false;
}
}
This option works fine until the mouse hovers over the Button
. At that moment both events begin to alternate uncontrollably and the only visible effect is that the button cannot be pressed. The truth is that it makes all the sense in the world: if the mouse hovers over it, Button
technically it is leaving the TextBox
, so the event is fired MouseLeave
. The latter causes the mouse to become inside the TextBox
, which triggers the event MouseEnter
and starts over.
How could I control this situation?
When the event is fired
MouseLeave
, check if the mouse is still over theTextBox
using theClientRectangle
del propertyTextBox
and the (static) propertyControl.MousePosition
as follows:To make sure the button is hidden when the mouse is released, also run this update when the event
MouseLeave
for the button is fired: