Click here to Skip to main content
15,503,359 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I need help with drawing of a line in C# on a panel. However how it must work is, you need to be able to place a picture on a panel then when that picture is placed on the panel, I need to be able to draw a line from the center of that picture to as far down as the user moves his/her mouse...

I know how to place the picture box and I also know how to draw lines, the problem comes in with the fact that both the picture box and the line are using mouse co-ordinates and mouse movement events on the panel. Which I think is what is messing it up.

Can someone maybe help me by giving some sort of suggestion as to how I can go about drawing this line? Thanks.
Andrewpeter 21-Sep-12 5:21am    
You use CreateGraphics() method to create "drawing paper", after that you use DrawLine() of graphics library to draw line on it. Good luck!

1 solution

Hi Storxstar,

I used this one to draw lines on a form: visual Basic Powerpacks 3.0[^]. Download it, add it to your toolbar, and use the LineShape tool, to draw lines on your form.

However, you won't be able to create a new form derived from the form on which you drew the line. But if you don't need to derive any forms from it, it's a perfect solution!
Share this answer
Frans Jan 21-Sep-12 5:26am    
But I see it won't cover the part on drawing from the users mouse... Sorry, read your question too fast I guess
Storxstar 21-Sep-12 5:35am    
Hi FransJan, I have already tried using the Lineshape tool, it works great, however the user needs to be able to draw the line with mouse movements, they also need to be able to draw the line diagonally. Can you use the lineshape tool with mouseEvents?
Frans Jan 21-Sep-12 6:05am    
I guess you need to find a way to get it worked with mouse events yes. Think you should create a button that needs to be pressed if the user wants to draw a line (if you don't have already). Then set visibility of lineshape to false, make it visible when draw button is clicked. Also, set default X1,X2,Y1,Y2 to 0, so you won't see any line when the button is clicked (although you've set the visibility true). set LineShape1.X1 = Mouse.Position.X; and LineShape1.Y1 = Mouse.Position.Y;
From here you need to figure a way to set the LineShapeX2 and the LineShape.Y2 to the new position of the mouse. Perhaps work with left-button for start position and right-button for end position?
Storxstar 21-Sep-12 7:36am    
Thanks for all your help Frans Jan, this has guided me in the right direction. I will post a solution if and when I manage to solve it.

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