Click here to Skip to main content
11,933,854 members (36,371 online)
Click here to Skip to main content
Add your own
alternative version


36 bookmarked

Drawing Lines And Rectangles Using the Mouse

, 17 Mar 2003
Rate this:
Please Sign up or sign in to vote.
Simple program utilizing the mouse to draw lines and rectangles.

Sample Image - linerect.jpg


This is my first program submittal to this site. I wrote this tiny snippet of code to show how to draw simple lines and rectangles in VS.NET using C#. The program is not perfect. The main problem to the procedure is, it doesn't bound the user from drawing beyond the form's boundary. Perhaps when I have time, I'll look into the ability to limit the selection process to the form. One solution would be to check the mouse pointer location on the form and restrict the user from going beyond the form's boundary. If anyone comes up with a better idea, please post your comments or solutions.

There were several submittals to this site regarding drawing selection rectangles and rubberband lines. For those of you who ran across these submittals, you might want to take a look at this tiny program to get some ideas.

The main trick to this program is in the mouse move:

if (((RadioButton)thisform.Controls[0]).Checked) 
                           thisform.PointToScreen(pe), Color.Black);
   pe = new Point(e.X, e.Y);
                           thisform.PointToScreen(pe), Color.Black);
                           Color.Black, FrameStyle.Dashed);
   SelectRect.Width = e.X - SelectRect.X;
   SelectRect.Height = e.Y - SelectRect.Y;
                           Color.Black, FrameStyle.Dashed);

The DrawReversible procedures toggle the drawing between the specified color and the background color. Also, you will need to attain the screen coordinates in order to begin drawing where the mouse is pointed to on the form.

I hope this program helps beginning control builders to gain some ideas on .NET drawing procedures.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
United States United States
A software programmer hobbyist who have been developing for a number of years spanning multiple languages and platforms.

You may also be interested in...

Comments and Discussions

QuestionForm cleaned when it minimized and remaximized Pin
Member 906895426-Jul-15 8:47
memberMember 906895426-Jul-15 8:47 
GeneralDrawing Lines And Rectangles Using the Mouse in Picture Box Pin
MrKyaw15-Jul-12 23:03
memberMrKyaw15-Jul-12 23:03 
GeneralMy vote of 5 Pin
takerman26-Apr-12 11:12
membertakerman26-Apr-12 11:12 
GeneralMy vote of 5 Pin
manoj kumar choubey20-Feb-12 1:04
membermanoj kumar choubey20-Feb-12 1:04 
Generalwe can correct this example by this code Pin
yummysms16-Feb-10 2:12
memberyummysms16-Feb-10 2:12 
QuestionWhere is the source code? Pin
RDurando6-Nov-08 0:18
memberRDurando6-Nov-08 0:18 
AnswerRe: Where is the source code? Pin
Sickness16-Feb-09 8:11
memberSickness16-Feb-09 8:11 
GeneralCursor.Clip Pin
tomzhu102413-Dec-07 23:07
membertomzhu102413-Dec-07 23:07 
GeneralMore Explaination! Pin
robert11029-Sep-06 7:00
memberrobert11029-Sep-06 7:00 
While I think that the functionality of this program is neat I do believe that the author should have given a little more explanation as to what was going on in the code. True, we can sit there and figure it out! But I think it is author’s moral and ethical responsibility to provide a comprehensive explanation so that even the very new programmers/students can understand the code without having to open another book / article to understand this one. Thanks.
GeneralRedrawing lines and rectangle Pin
mitreviper11-May-04 0:49
membermitreviper11-May-04 0:49 
GeneralRe: Redrawing lines and rectangle Pin
<div style='color:#6699cc; '>MadHatter ¢</div>21-Jan-05 18:53
MadHatter ¢
21-Jan-05 18:53 
GeneralStopping th cursor leaving the form Pin
Roger Allen19-Mar-03 7:18
memberRoger Allen19-Mar-03 7:18 
GeneralRe: Stopping th cursor leaving the form Pin
Anonymous5-Jan-04 5:42
sussAnonymous5-Jan-04 5:42 
GeneralComments. Pin
73Zeppelin18-Mar-03 17:14
member73Zeppelin18-Mar-03 17:14 
GeneralRe: Comments. Pin
Bill Lien18-Mar-03 21:58
sussBill Lien18-Mar-03 21:58 
GeneralRe: Comments. Pin
Vlad Vissoultchev19-Mar-03 5:37
memberVlad Vissoultchev19-Mar-03 5:37 
GeneralArticle format Pin
Steve McLenithan18-Mar-03 16:57
memberSteve McLenithan18-Mar-03 16:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151126.1 | Last Updated 18 Mar 2003
Article Copyright 2003 by coolshot26
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid