'button1_Click'没有重载匹配委托'System.EventHandler'

时间:2012-06-23 02:36:09

标签: c#

我有这段代码:

 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Forms;

 namespace WindowsFormsApplication1
 {
     public partial class Form1 : Form
     {
         public Form1()
         {
             InitializeComponent();
         }
         public class vars
         {
             public static int x = 250;
             public static int y = 250;
         }
         private void button1_Click(object sender, EventArgs e)
         {
             button1.Visible = false;
             Pen main = new Pen(Color.Black);
             this.Graphics.DrawRectangle(main, vars.x, vars.y, 2, 2);
             while (true)
             {

             }
         }
      }
 }

但是我不断收到一个令人讨厌的错误(没有超载,因为我没有知道如何接近,按钮1&Click'匹配委托' System.EventHandler')。我一直在网上搜索和解决问题一段时间,并没有找到任何答案。任何建议将不胜感激。

1 个答案:

答案 0 :(得分:3)

删除代码的PaintEventArgs g部分。附加到事件的方法必须与事件的“模式”匹配。

在您自己的类之外声明变量,除非您在Form1之外需要它们。它只是不必要的。

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        int x = 250;
        int y = 250;

        public Form1()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
       {
            button1.Visible = false;
            Pen main = new Pen(Color.Black);
            this.CreateGraphics().DrawRectangle(main, vars.x, vars.y, 2, 2);
       }
    }
}

这应该有用!