Click here to Skip to main content
15,921,028 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm creating a line.

with this code:

gr.DrawLine(the_pen, 50, 10, 250,10)

Now I want to know ,How i can define MouseMove Event for the line?

Updated 27-Nov-11 8:42am
OriginalGriff 27-Nov-11 15:09pm    
You need to improve that question - at the moment it doesn't make any sense. What do you want to do with DrawLine that the mouse is going to affect? Draw from the last point to the current? From the first? Or store them all and draw them all? Or something else altogether?
Use the "Improve question" widget to edit your question and provide better information.
Sergey Alexandrovich Kryukov 27-Nov-11 15:19pm    

My other question is: do you use "override void OnPaint"? "Paint" event?

There is no such thing. The line is not an object of a type implementing events. Moreover, the line is not an object at all.

All you can do is adding an event handler to the control used to draw your line on. Alternatively, you can override mouse-handling virtual methods. How this event is related to the line — is all your business. You can define some criteria used to find out of a mouse is "close" to some point of a line at the moment of pressing a mouse key down. The schema is this:

class MyContol : System.Windows.Forms.Control {

    public MyContol() {
        ThePen = new System.Drawing.Pen(/*...*/); // to be disposed
        //important to avoid flicker:

    protected override void Dispose(bool disposing) {
        if (disposing)
            ThePen.Dispose(); //imporant!

    protected override void OnPaint(PaintEventArgs e) {
        System.Drawing Point p1 = // get from your data model
        System.Drawing Point p2 = // get from your data model
        e.Graphics.DrawLine(ThePen, p1, p2);

    protected override void OnMouseDown(MouseEventArgs e) {
        //check up with your data model
    protected override void OnMouseUp(MouseEventArgs e) {
        //check up with your data model
    protected override void OnMouseMove(MouseEventArgs e) {
        //check up with your data model
        if (/*...*/)
           Invalidate(); //important, otherwise WM_PAINT is not sent, OnPaint not called

    System.Drawing.Pen ThePen;

The key is your data model here: you need to represent all you draw in some pure-data model and keep in the memory of you class. Update of the model by mouse, keyboard, controls, etc. needs Invalidate to be called. You can use Invalidate with a parameter (Rectangle or Region) to redraw only a part of the scene.

A bonus advice: implement your rendering method as a separate method of the same class, make a parameter of the type Graphics. In this way, you can call it no only from OnPaint, but from somewhere else using different instance of the Graphics: of a bitmap for exporting the graphics to a bitmap, of a printer document to print it, etc.

Share this answer
Sander Rossel 27-Nov-11 16:15pm    
Good advice, my 5.
Check your spelling in the OnMouseMove Method... Here come the PAIN! :)
Sergey Alexandrovich Kryukov 27-Nov-11 16:46pm    
Thank you very much, Naerling, spelling fixed, but not anymore: this matter is paintful. :-)
Sergey Alexandrovich Kryukov 27-Nov-11 16:50pm    
Note: this is so close to the recent question you have answered: implementing event triggered when a variable changed. Line in rendering is a similar thing: there is no such event; an even should be create outside.
Sander Rossel 27-Nov-11 17:22pm    
Yeah, I noticed. So where's you custom EventArgs? ;)
Sergey Alexandrovich Kryukov 27-Nov-11 21:40pm    
They are already defined: PaintEventArgs and MouseEventArgs and the events are already defined, that's the difference.

In you case, you (correctly) advised to create a type with event, and, some parameters (I advised) are needed, so you have to create custom event arguments type and (say, ValueUpdateEventArgs<T> : System.EventArgs {/*...*/}, so event declaration will be public event System.Event<ValueUpdateEventArgs<string>> StringValueModified.

Is that clear?
You may upload a sample for me. I do not use.
Share this answer
RaviRanjanKr 29-Nov-11 15:38pm    
A suggestion :- its not a good practice to post your feedback or comment as an answer. you can use have a question or Comment.

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