Hi Experts!
I am working on drawing in C#. I have a problem in this program; when I draw a new object on mouse event and paint event the previous drawn object disappears. My code is as follows.

private void picMain_MouseDown(object sender, MouseEventArgs e)
        {
            MouseDownPoint = e.Location;
            mouseMovePoint = e.Location;
        }

private void picMain_MouseMove(object sender, MouseEventArgs e)
        {
            mouseMovePoint = e.Location;
        }
private void picMain_Paint(object sender, PaintEventArgs e)
        {
            G.DrawLine(new Pen(Color.Green, 10),MouseDownPoint, mouseMovePoint);
        }

Please help me to draw an unlimited number of objects without erasing the previous one.
The object in my case may be any drawing object e.g rectanlge, ellipse, line,polygon etc. is there any method to store all the drawn object in arraylist or list. if so then please guide me.

Your Paint event handler does just what you told it to do: Draw a green line with a width of ten, from the point you did a mousedown until the point you released the mouse.
Reread the advise Ryshad gave you in your previous thread about this subject: http://www.daniweb.com/forums/thread261808.html

commented: thanks :p +1

A good way to store your drawn objects would be indeed an arrayList, however for that you would need to create a class that contains the informations related to your drawings. Once you get that, you can use a foreach loop to redraw each of them.

The paint event repaints the whole component which is why your components get erased. you could use the mouse up event instead to add the content to the form

As ddanbe pointed out, i gave you the outline for a set of class and subclasses to store each item you draw.

Then use a List<DrawnItem> to store each of the objects you draw. You can then redraw them in the Paint method using foreach(DrawnItem item in ListofItems). In your last thread you wanted to be able to move/resize them, you can do that using the List as well.

This is a fun project. So long as you stay away from zooming and panning nothing gets complicated.

Programming is a lot more than knowing how the code works. Its more important to understand the process you are trying to emulate. Ryshad has already pointed out you need a collection to hold all the objects you want to draw, and when its time to draw. draw each one of them one at a time. Its obvious in your code you are leaving out some of what you are doing. a more clear cut sudo for simple lines only follows.

+when mouse down make note of the fact you are drawing and save the point you started.

+on mouse move save the point where the mouse currently is for preview drawing.

+on mouse up make note of the idea that you are no longer drawing. use the start point and the point of mouse up to up to create a new line object and add it to a collection of line objects.

+on paint - if you are drawing, draw the start and end points this show the preview of the line, either way loop through the collection of line objects and draw each one.

Understanding what must be done is the most important part. Turning this into code is simple. Adding the ability to move and resize the objects it just adding complications to the same events.

This could be a fun thing to do. Enjoy the learning experience.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.