【发布时间】:2011-09-26 08:35:10
【问题描述】:
由于我对 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());
}
}
【问题讨论】:
-
事件处理程序委托有一个
object sender, EventArgs e参数列表。你的没有。为了记录,我怀疑Click事件会有PaintEventArgs。 -
看起来您已将 OnPaint() 方法重命名为 klik,然后尝试将其连接到按钮事件处理程序。删除 += klik 分配,并在表单设计器中双击按钮,它将创建一个新的按钮处理程序。
-
这是试错编程。会有更多的错误,你不能只画一个位图就希望它存活下来。访问您当地的图书馆并查看 Winforms 编程书籍。
标签: c#