|
Hello,
I replied to your query in another forum. Just for reference to this forum, I will reply again.
In your code sample, the problem was with the calculation of height and width of the rectangle. When you move the mouse from Top Left to Right Bottom, the values are calculated correctly. However, while going in the opposite version, either height or Width or even both gets calculated in negative values. As a result, no rectangle is drawn. Try the given code in MouseUp() event.
BEGIN CDOE
private void Form1_MouseUp(object sender, MouseEventArgs mouseEv) {
textBox2.Text = Convert.ToString(mouseEv.Location);
Graphics graphics = this.CreateGraphics(); Rectangle rectangle;
if (mouseEv.Y > mouseDownY) { height = mouseEv.Y - mouseDownY; } else height = mouseDownY - mouseEv.Y; if (mouseEv.X < mouseDownX) { width = mouseDownX - mouseEv.X; rectangle = new Rectangle(mouseEv.X, mouseEv.Y, width, height); } else { width = mouseEv.X - mouseDownX; rectangle = new Rectangle(mouseDownX, mouseDownY, width, height); }
graphics.DrawRectangle(Pens.Red, rectangle); }
END CODE
I hope this will help.
Regards,
|