没有重载的匹配委托’system.eventhandler’

因为我对C#很陌生,所以我很难用下面的代码。 当我点击按钮’knop’时,必须执行方法’klik’。 该方法必须在窗体上绘制由’DrawMandel’生成的位图’b’。 但我不断得到错误’没有重载的代理’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()); } } 

您需要将public void klik(PaintEventArgs pea, EventArgs e)更改为public void klik(object sender, System.EventArgs e)因为没有带参数PaintEventArgs pea, EventArgs e Click事件处理程序。

是的,Click事件处理程序(klik)存在问题 – 第一个参数必须是对象类型,第二个参数必须是EventArgs。

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

如果要在窗体或控件上绘制,请使用CreateGraphics方法。

 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); } 

您需要包含按钮单击处理程序以匹配模式

 public void klik(object sender, EventArgs e) 

更改klik方法如下:

 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); }