C
C#3mo ago
Hazzza

How do I detect if the mouse position is outside a panel and its children in WinForm

The panel in this case is the blue box behind the per 100g and per item buttons. I was going to use a mouse.Leave event to trigger when to hide my drop down menu however if the mouse 'leaves' the panel and hovers over the per 100g button it would trigger the event and hide the menu. So instead I trying to check if the mouse is outside the panels boundaries before hiding however the mouse location is much greater than the panel position even though the mouse is inside the panel. Anyone know how to convert the 2 positions into the correct values for this case? Thanks
C#
Point MousePos = MousePosition;
Point pnlPos = pnlPriceChoice.Location;
Size pnlSize = pnlPriceChoice.Size;

Console.WriteLine($"Mouse Pos: {MousePos.X}, {MousePos.Y} \n LowerBound: {pnlPos.X}, {pnlPos.Y} \n UpperBound: {pnlPos.X + pnlSize.Width}, {pnlPos.Y + pnlSize.Height}");

if (!(MousePos.X >= pnlPos.X && MousePos.X <= pnlPos.X + pnlSize.Width && MousePos.Y >= pnlPos.Y + pnlSize.Height && MousePos.Y <= pnlPos.Y))
{
pnlPriceChoice.Visible = false;
}
C#
Point MousePos = MousePosition;
Point pnlPos = pnlPriceChoice.Location;
Size pnlSize = pnlPriceChoice.Size;

Console.WriteLine($"Mouse Pos: {MousePos.X}, {MousePos.Y} \n LowerBound: {pnlPos.X}, {pnlPos.Y} \n UpperBound: {pnlPos.X + pnlSize.Width}, {pnlPos.Y + pnlSize.Height}");

if (!(MousePos.X >= pnlPos.X && MousePos.X <= pnlPos.X + pnlSize.Width && MousePos.Y >= pnlPos.Y + pnlSize.Height && MousePos.Y <= pnlPos.Y))
{
pnlPriceChoice.Visible = false;
}
No description
No description
3 Replies
ChaChaCha
ChaChaCha3mo ago
wait i dont understand maybe my english sucks if it leaves the blue panel what do you want it to do hide the panel or what also isnt there a Control.PointToClient(MousePos) thing which gives it relaitve to your mouse
Hazzza
HazzzaOP3mo ago
Thank you for showing interest, i have managed to fix this issue now and it all works fine, i had to use a Control.PointToScreen to fix it
ChaChaCha
ChaChaCha3mo ago
Oh thats what its called Good job man
Want results from more Discord servers?
Add your server