0

我有以下代码:

public partial class Painter : Form
    {
        private System.ComponentModel.Container Components = null;

        private const int m_intDIAMETER = 8;

        private const int m_intMOUSEUP_DIAMETER = 4;

        private Graphics m_objGraphic;

        private bool m_binShouldPaint = false;

        private bool m_binShouldErase = false;



        public Painter()
        {
            InitializeComponent();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                    //components = null;
                }
            }
            base.Dispose(disposing);
        }
        static void Main()
        {
            Application.Run(new Painter());
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            m_objGraphic = CreateGraphics();

        }

        private void Painter_MouseDown(object sender,System.Windows.Forms.MouseEventArgs e)
        {
            //m_objGraphic.FillEllipse(new SolidBrush(Color.HotPink), e.X, e.Y, m_intDIAMETER, m_intDIAMETER);
            //m_binShouldPaint = true;

            if (e.Button == MouseButtons.Left)
            {
                m_binShouldPaint = true;
            }
            else if (e.Button == MouseButtons.Right)
            {
                m_binShouldErase = true;
            }
        }

在编译时,我的调试器生成以下错误:

Error   1   Type 'Painter.Painter' already defines a member called 'Dispose' with the same parameter types

我认为 Dispose 方法是由程序生成的,这就是为什么当我编写它而不生成“Dispose”时它会给我一个错误。但是我该如何解决呢?

4

1 回答 1

2

您的设计器生成的文件中有另一种Dispose方法。即Painter.Designer.cs,如果您有一些自定义实现Dispose,则修改Painter.Designer.cs文件中的一个或将其移动到您的代码后面。

Visual Studio 生成一个designer.cs带有每个 的文件Form,该文件包含有关表单上使用的控件的代码,并且还具有Dispose方法实现。由于您的代码后面有一个,因此您会收到一个错误,即Dispose在同一个类中有多个方法。(两个文件通过partial关键字具有相同的类)

在此处输入图像描述

于 2013-10-22T20:21:41.573 回答