physicalattraction
physicalattraction

Reputation: 6858

How to add a custom EventHandler to the PaintEventHandler?

As a C# exercise, I want to add a custom PaintEventHandler that draws a rectangle to my form whenever it is drawn. I have the following:

1) A delegate

public delegate void MyPaintFormEventHandler(object sender, PaintEventArgs e);

2) A method to be executed, drawing the rectangle.

private void draw_red_rectangle(object sender, PaintEventArgs e)
{ /* Implementation here */ }

3) A subscription to the Paint event of the Form.

this.Paint += new MyPaintFormEventHandler(draw_red_rectangle);

However, this piece of code together does not compile, for the following reason:

Cannot implicitly convert type 'use_graphics.MyPaintFormEventHandler' to 'System.Windows.Forms.PaintEventHandler'

Can anyone help me out by pinpointing what I am doing wrong and how I could fix this?

Upvotes: 0

Views: 3039

Answers (1)

Fred Deschenes
Fred Deschenes

Reputation: 71

Your issue comes from the fact that you have declared your own delegate type. You can simply use :

this.Paint += new PaintEventHandler(draw_red_rectangle);

or

this.Paint += draw_red_rectangle;

Upvotes: 1

Related Questions