Download project
I am trying to make a panel with a background color that should be available at runtime when the user holds the left mouse button and moves it. All work is detected when the user starts from the upper left position and goes to the lower right, as shown in the figure:
But I want the user to be able to make a panel from bottom right to top left. Just like when you select something on your computer with the mouse

Here is my code:
public void parent_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
Point tempLoc = e.Location;
this.Location = new Point
(
Math.Min(this.Location.X, tempLoc.X),
Math.Min(this.Location.Y, tempLoc.Y)
);
this.Size = new Size
(
Math.Abs(this.Location.X - tempLoc.X),
Math.Abs(this.Location.Y - tempLoc.Y)
);
this.Invalidate();
}
}
I think I'm wrong here and I just can't find the right algorithm for it:
this.Size = new Size
(
Math.Abs(this.Location.X - tempLoc.X),
Math.Abs(this.Location.Y - tempLoc.Y)
);
But if I use a rectangle, it works fine, but I want my panel to be able to do this too.
source
share