因爲我對C#很陌生,所以我與下面的一段代碼糾纏在一起。當我點擊按鈕'knop'時,必須執行'klik'方法。該方法必須在窗體上繪製由'DrawMandel'生成的位圖'b'。但我總是得到錯誤'沒有超載匹配委託'system.eventhandler'。沒有超載的匹配委託'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());
}
}
事件處理程序委託有一個'對象發件人,EventArgs e'參數列表。你的不是。對於記錄,我懷疑'Click'事件會有'PaintEventArgs'。 – BoltClock
看起來您已將OnPaint()方法重命名爲klik,然後嘗試將其連接到按鈕事件處理程序。除去+ = klik賦值,並從窗體設計器中雙擊該按鈕,它將創建一個新的Button處理程序。 – StuartLC
這是試錯法編程。會有更多的錯誤,你不能只畫一個位圖,並希望它倖存下來。訪問您當地的圖書館並查看Winforms編程手冊。 –