Search code examples
c#graphicsdrawellipserect

Draw object on same Graphics C#


 private void main_pic_Paint(object sender, PaintEventArgs e)
    {
       g = e.Graphics;
        if (rect_bt_clicked)
        {
            if (_r >= 0)
            {
                for (int j = 0; j <= _r; j++)
                {
                    rect = new Rectangle(
                        RectArray[j].SP.X,
                        RectArray[j].SP.Y,
                        RectArray[j].EP.X - RectArray[j].SP.X,
                        RectArray[j].EP.Y - RectArray[j].SP.Y);
                    Brush b = new SolidBrush(Color.Red);
                    Pen p = new Pen(Color.Blue, 2);
                    g.FillRectangle(b, rect);
                    g.DrawRectangle(p, rect);

                }
            }
        }
        if (ellip_bt_clicked)
        {
            if (_e >= 0)
            {
                for (int j = 0; j <= _e; j++)
                {
                    rect = new Rectangle(
                        EllipArray[j].SP.X,
                        EllipArray[j].SP.Y,
                        EllipArray[j].EP.X - EllipArray[j].SP.X,
                        EllipArray[j].EP.Y - EllipArray[j].SP.Y);
                    Brush b = new SolidBrush(Color.Red);
                    Pen p = new Pen(Color.Blue, 2);
                    g.FillEllipse(b, rect);
                    g.DrawEllipse(p, rect);

                }
            }
        }

    }

I have two buttons. One for draw rectangle and one for draw ellipse. But when i click draw rect. after that i click draw ellipse. It not work in one graphics. I don't know how to fix. Sorry about my English.


Solution

  • You need to draw EVERYTHING in the Paint event handler EVERY time. What you normally would do is store all the data that represents the entire drawing in one or more member variables and then, in the Paint event handler, you read that data and draw the drawing.

    If you want to draw one rectangle and one ellipse then I would suggest you declare one or more variables to represent the rectangle and then one or more variables to represent the ellipse. That would include a variable to to indicate whether or not that shape is to be drawn, which would be false for both to start with. When you click the rectangle button you would set all the rectangle fields and when you click the ellipse button you would set all the ellipse fields.

    If you want to draw multiple rectangles and multiple ellipses then I would suggest that you define a class to represent each of them, possibly inheriting a common base class for the common functionality. You would then declare two variables to store a collection of each type. When you click the rectangle button you add an instance of one type to the collection of rectangles and when you click the ellipse button you add an instance of the other type to the ellipse collection.

    In either case, after putting the appropriate data in the appropriate place, you would call Refresh or, preferably, Invalidate and Update on the control you want to draw on. That will raise the Paint event and invoke the Paint event handler. In the handler, you read the data from the appropriate place and do the drawing.