no overload for matches delegate 'system.eventhandler'

匿名 (未验证) 提交于 2019-12-03 02:13:02

问题:

As I'm pretty new to C#, I struggle with the following piece of code. When I click to button 'knop', the method 'klik' has to be executed. The method has to draw the Bitmap 'b', generated by 'DrawMandel' on the form. But I constantly get the error 'no overload for matches delegate 'system.eventhandler'.

using System; using System.Windows.Forms; using System.Drawing;  class Mandelbrot : Form  {     public Bitmap b;     public Mandelbrot()      {         Button knop;         knop = new Button();                 knop.Location = new Point(370, 15);                 knop.Size = new Size(50, 30);         knop.Text = "OK";                  this.Text = "Mandelbrot 1.0";         this.ClientSize = new Size(800, 800);         knop.Click += this.klik;         this.Controls.Add(knop);               }     public void klik(PaintEventArgs pea, EventArgs e) {         Bitmap c = this.DrawMandel();         Graphics gr = pea.Graphics;         gr.DrawImage(b, 150, 200);     }     public Bitmap DrawMandel()     {         //function that creates the bitmap         return b;     }     static void Main() {         Application.Run(new Mandelbrot());     }  } 

回答1:

You need to change public void klik(PaintEventArgs pea, EventArgs e) to public void klik(object sender, System.EventArgs e) because there is no Click event handler with parameters PaintEventArgs pea, EventArgs e.



回答2:

Yes there is a problem with Click event handler (klik) - First argument must be an object type and second must be EventArgs.

public void klik(object sender, EventArgs e) {   // } 

If you want to paint on a form or control then use CreateGraphics method.

public void klik(object sender, EventArgs e) {     Bitmap c = this.DrawMandel();     Graphics gr = CreateGraphics();  // Graphics gr=(sender as Button).CreateGraphics();     gr.DrawImage(b, 150, 200); } 


回答3:

You need to wrap button click handler to match the pattern

public void klik(object sender, EventArgs e) 


回答4:

Change the klik method as follows:

public void klik(object pea, EventArgs e) {     Bitmap c = this.DrawMandel();     Button btn = pea as Button;     Graphics gr = btn.CreateGraphics();     gr.DrawImage(b, 150, 200); } 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!