Click here to Skip to main content
15,668,517 members
Please Sign up or sign in to vote.
1.67/5 (2 votes)
See more:
how to draw Rectangle using mouse on the form..
fkassaie 3-Nov-13 3:06am    
I think these link will be useful for you:

Since you refer to "on the form" I will assume this is not VB.NET - which is broswer based - but WinForms, which isn't.

It's not too difficult:
1) Handle the MouseDown, MouseMove and MouseUp events for teh form. Also Handle the Paint event.
2) Create three class level variables: a Point called "start", a Point called "current" and a bool called "moving".
3) In the MouseDown handler, set moving to true and start to the current mouse position.
4) In the MouseUp event, set moving to false
5) In the MouseMove event, look at moving. If it is true, set current to the current mouse position, and call the Invalidate method.
6) Invalidate causes the form to be re-drawn, causing the Paint event handler to be called.
7) In the Paint handler, use the PaintEventArgs parameter Graphics property, and call it's DrawRectangle method - it has an overload that takes two points.

Share this answer
Hi, I think these link will be useful for you:
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900