Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
How could I now If I clicked the lines that I drawn? Are lines can be distinguish as objects? :confused:
Posted

I assume you drew the lines using Graphics.DrawLine?
If so, then no, they are not separate objects, and you cannot click them.

If you need to, then you need to maintain a list of where they start and end - presumably you do this anyway so that you can re-draw them each time in the Paint event, otherwise they will disappear.

You can then check if where you clicked is on the line in your form or panel event.

Be aware though, it is only simple to do if the line is horizontal or vertical: if it slopes, you will have to work out exactly how it is drawn to be sure if a click on on or off a particular line!

What are you trying to do: there may be an easier way to achieve it.
 
Share this answer
 
Comments
yummy02 25-Jan-11 3:28am    
i have 3 lines in the form and all of them are created using Graphics.DrawLine. How can I maintain a list where lines starts and ends? Thanks OriginalGriff... :)
yummy02 25-Jan-11 3:31am    
Is there anyway that I can drawn a line that i clickable and movable?
Sergey Alexandrovich Kryukov 25-Jan-11 3:47am    
Reasonable explanation - my 5. I'm trying to add a bit.
yummy02 25-Jan-11 4:58am    
+5 :)
This is not very easy. This problem is solved in WPF which I already recommended to switch to. You progress in System.Windows.Forms gets slower as your problem accumulate.

A problem of like click test is one of them. You mouse click event fires when you click, but you need ignore it if no object is clicked. In case of line (thin object) it's important to define an area around a line which is considered its vicinity. It should be something wider then 1 pixel, otherwise the user will almost never hit it. If you're talking a straight line segment, you may need a rectangular area that covers your line. The definition of required width of vicinity (usually definitions works with "radius") is a matter of trade-of between resolution and ease of access. Having it variable (depending on scale) or configurable (or both) makes sense.
 
Share this answer
 
Comments
yummy02 25-Jan-11 4:58am    
+5 :)
Sergey Alexandrovich Kryukov 6-Feb-11 17:49pm    
Epsen recently informed me that you forget to actually do this vote :-)
--SA
yummy02 6-Feb-11 20:23pm    
Yes, last time I forgot to vote. Sorry :)
Sergey Alexandrovich Kryukov 6-Feb-11 22:39pm    
Oh, no problem at all and than you.
--SA
Espen Harlinn 6-Feb-11 14:34pm    
He forgot to vote, so have a 5 - btw: good 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