Search code examples
c#.netpaintevent

How to create CustomEventArgs from PaintEventArgs by inheritance


I want to create a simple dial in a user interface to indicate the angle of a rotation. The dial is drawn in a panel object. Via calling the invalidate() function of the panel I want to raise the paint event and get the angular position updated. I want to pass the angle of the dial arm as a parameter with the EventHandler's arguments. For some reason I cannot create a class that inherits from PaintEventArgs.

   public class AnglePaintEventArgs : PaintEventArgs
{
    int angle;
    public AnglePaintEventArgs(int Angle )
    {
        angle = Angle;
    }
}

}

I get tis error message: CS7036 There is no argument given that corresponds to the required formal parameter 'graphics' of 'PaintEventArgs.PaintEventArgs(Graphics, Rectangle)'

In the next step I want to use AnglePaintEventArgs to pass it to the panel's paint event.

Here the function to be execute from the paint event:`

private void panel1_Paint(object sender, AnglePaintEventArgs e)

    {

        //in the Paint event we *ONLY* Paint!!!

        if (_showGraphic)

        {
            Point center = new Point(75, 75);
            Pen myPen = new Pen(Brushes.Red);
            myPen.Width = 30;
            
            int x_end = Convert.ToInt32(Math.Cos(angle) * 75);
            int y_end = Convert.ToInt32(Math.Sin(angle) * 75);
            Point endPoint =new Point(x_end+center.X, y_end+center.Y);

            e.Graphics.FillEllipse(Brushes.Yellow, 0, 0, center.X *2, center.Y*2);
            e.Graphics.DrawLine(myPen, center, endPoint);

        }

    }

And the connection to the panel's paint event

this.panel1.Paint += panel1_Paint;

Using an variable for angle in the form class would be a workaround. I think the correct way is to pass the angle via the eventargs. Please advise.


Solution

  • It seems you need to read more about inheritance and it's syntax in C#. The error is exactly on point:

    CS7036 There is no argument given that corresponds to the required formal parameter 'graphics' of 'PaintEventArgs.PaintEventArgs(Graphics, Rectangle)

    Let's break it down:

    1. "There is no argument given that corresponds to the required formal parameter 'graphics'" - your code fails to build, because you have not provided value for a 'graphics' parameter

    2. "of 'PaintEventArgs.PaintEventArgs(Graphics, Rectangle)' - this is meant to help you identify which parameter is not supplied:

    PaintEventArgs - this is the type that causes the error

    PaintEventArgs (after dot) is the name of the method. In this case it's the same as the class type, which means that is it's constructor method. Following are the types of it's parameters - Graphic and Rectangle

    To fix your code you need to change:

    public class AnglePaintEventArgs : PaintEventArgs
    {
        int angle;
    
        public AnglePaintEventArgs(int Angle )
        {
            angle = Angle;
        }
    }
    

    to

    public class AnglePaintEventArgs : PaintEventArgs
    {
        int angle;
    
        public AnglePaintEventArgs(Graphic graphic, Rectangle rectangle) : base(graphic, rectangle)
        {
            angle = Angle;
        }
    }
    

    to supply the required arguments of the base type.