2013-10-22 30 views
0

我有以下代码:在C#“处置”模糊误差

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方法程序,这就是为什么当我写它而不产生“处置”它给我一个错误。但我该如何解决它?

+0

表单类已经有一个Dispose()方法,存储在设计文件中。这看起来像.NET 1.x代码中的一个剪切粘贴事故,你不希望使用你发布的Dispose()方法。只要删除它。 –

回答

2

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

Visual studio每Form生成一个designer.cs文件,该文件包含有关窗体上使用的控件的代码,并且还有Dispose方法实现。由于你的代码有一个,所以你在同一个类中有多个Dispose方法的错误。 (这两个文件具有相同的类通过partial关键字)

enter image description here

+0

如何找到designer.cs文件?我只有两个文件是Form1.cs和Form1.cs [Design],它只显示我的Painter表单。 – Nathalie

+0

@muggleBlood,在解决方案资源管理器中展开您的表单,您将看到设计器文件。我只是添加了一个屏幕快照显示。 – Habib

+0

修改我的Dispose方法后,我的程序编译时没有错误,但它也没有生成我打算执行的操作。我做了我的代码工作,完全删除我的Form1.cs文件Dispose方法以及私人System.Windows.Forms.Container组件= null ;.之后,我的程序编译没有错误,并产生了结果 – Nathalie