简体   繁体   English

我该如何修复错误:Type Form1'已经定义了一个名为'Dispose'的成员,它具有相同的参数类型?

[英]How can i fix the error: Type Form1' already defines a member called 'Dispose' with the same parameter types?

Tried to change the methods names also tried to create new class and put the code from form1 there but nothing worked. 尝试更改方法名称也尝试创建新类并将form1中的代码放在那里但没有任何效果。

This is the code in form1: 这是form1中的代码:

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;
using VirtualBox;
using System.Runtime.InteropServices;

namespace Desktop
{
    public partial class Form1 : Form
    {

        #region DLLs
        [DllImport("user32.dll")]
        private static extern IntPtr CreateDesktop(string lpszDesktop, IntPtr lpszDevice, IntPtr pDevmode,
                                                   int dwFlags, long dwDesiredAccess, IntPtr lpsa);

        [DllImport("user32.dll")]
        private static extern bool SwitchDesktop(IntPtr hDesktop);

        [DllImport("user32.dll", EntryPoint = "CloseDesktop", CharSet = CharSet.Unicode, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool CloseDesktop(IntPtr handle);

        [DllImport("user32.dll")]
        public static extern bool SetThreadDesktop(IntPtr hDesktop);

        [DllImport("user32.dll")]
        public static extern IntPtr GetThreadDesktop(int dwThreadId);

        [DllImport("kernel32.dll")]
        public static extern int GetCurrentThreadId();
        #endregion

        #region Enumeratoren
        [Flags]
        internal enum DESKTOP_ACCESS_MASK : uint
        {
            DESKTOP_NONE = 0,
            DESKTOP_READOBJECTS = 0x0001,
            DESKTOP_CREATEWINDOW = 0x0002,
            DESKTOP_CREATEMENU = 0x0004,
            DESKTOP_HOOKCONTROL = 0x0008,
            DESKTOP_JOURNALRECORD = 0x0010,
            DESKTOP_JOURNALPLAYBACK = 0x0020,
            DESKTOP_ENUMERATE = 0x0040,
            DESKTOP_WRITEOBJECTS = 0x0080,
            DESKTOP_SWITCHDESKTOP = 0x0100,

            GENERIC_ALL = (DESKTOP_READOBJECTS | DESKTOP_CREATEWINDOW | DESKTOP_CREATEMENU |
                            DESKTOP_HOOKCONTROL | DESKTOP_JOURNALRECORD | DESKTOP_JOURNALPLAYBACK |
                            DESKTOP_ENUMERATE | DESKTOP_WRITEOBJECTS | DESKTOP_SWITCHDESKTOP),
        }
        #endregion

        public Form1()
        {
            InitializeComponent();

            Desktop("MyDesktop");
            System.Threading.Thread.Sleep(1000);
            Application.DoEvents();
            Desktop();
            Application.DoEvents();
            System.Threading.Thread.Sleep(3000);
            SwitchToOrginal();
            Application.DoEvents();
        }


        public void Dispose()
        {
            SwitchToOrginal();
            ((IDisposable)this).Dispose();
        }

        /// <summary>
        /// Unterklassen können hier die Funktionalität der Objektzerstörung erweitern. 
        /// </summary>
        /// <param name="fDisposing"></param>
        protected virtual void Dispose(bool fDisposing)
        {
            if (fDisposing)
            {
                // Hier die verwalteten Ressourcen freigeben
                //BspVariable1 = null;
                CloseDesktop(DesktopPtr);
            }
            // Hier die unverwalteten Ressourcen freigeben
        }

        void IDisposable.Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this); //Fordert das System auf, den Finalizer für das angegebenen Objekt nicht aufzurufen
        }

        #region Variablen
        IntPtr _hOrigDesktop;
        public IntPtr DesktopPtr;
        private string _sMyDesk;
        public string DesktopName
        {
            get
            {
                return (_sMyDesk);
            }
            set
            {
                _sMyDesk = value;
            }
        }
        #endregion

        #region Konstruktoren
        public void Desktop()
        {
            _sMyDesk = "";
        }

        public void Desktop(string sDesktopName)
        {
            _hOrigDesktop = GetCurrentDesktopPtr();
            _sMyDesk = sDesktopName;
            DesktopPtr = CreateMyDesktop();
        }
        #endregion

        #region Methoden
        public void show()
        {
            SetThreadDesktop(DesktopPtr);
            SwitchDesktop(DesktopPtr);
        }

        public void SwitchToOrginal()
        {
            SwitchDesktop(_hOrigDesktop);
            SetThreadDesktop(_hOrigDesktop);
        }

        private IntPtr CreateMyDesktop()
        {
            return CreateDesktop(_sMyDesk, IntPtr.Zero, IntPtr.Zero, 0, (long)DESKTOP_ACCESS_MASK.GENERIC_ALL, IntPtr.Zero);
        }

        public IntPtr GetCurrentDesktopPtr()
        {
            return GetThreadDesktop(GetCurrentThreadId());
        }
        #endregion

        private void Form1_Load(object sender, EventArgs e)
        {

        }


    }
}

The error is in the form1.designer.cs on this line: 该错误位于此行的form1.designer.cs中:

protected override void Dispose(bool disposing)

Tried to change the method name here and in form1 but it didn't work. 试图在这里和form1中更改方法名称,但它不起作用。

Neel is right it is a partial, 尼尔是对的,它是偏的,

If you need to do some job while dispose you can use some events such as OnDispose, Dispose +=... etc. as well. 如果你需要在处理时做一些工作,你可以使用一些事件,如OnDispose,Dispose + = ...等。 It is strange that dispose func. 配置功能很奇怪。 is generated automatically in designer file. 在设计器文件中自动生成。 Even you change designer file it will be generated again.So you can't override this overload method. 即使您更改设计器文件,它也会再次生成。因此您无法覆盖此重载方法。 just you can call it such like this.Dispose(true);... 只是你可以像这样调用它.Dispose(true); ...

Use this if you'll dispose something else , 如果你将其他东西丢弃,请使用它,

 public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.Disposed += (s, a) =>
            {
                //Dispose unmanaged stuffs etc.
            };
    }

BTW, I've tested after deleting designer.cs method doesn't cause regeneration for Windows Forms app. 顺便说一句,我在删除designer.cs方法后测试了不会导致Windows Forms应用程序的重新生成。 So it would be possible sol. 所以有可能是sol。 as well. 同样。 Mostly most of code generators replace the designer file when things change. 当事情发生变化时,大多数代码生成器都会替换设计器文件。 Such as EntityCode generators,SL generated codes. 如EntityCode生成器,SL生成的代码。

There's already a method called Dispose defined, check the designer generated code. 已经有一个名为Dispose的方法,检查设计器生成的代码。

Remove the Dispose from the form1.cs file... Did you notice that public partial class line there? 从form1.cs文件中删除Dispose ...您是否注意到公共部分类行? That means the class is spreaded in 2 or more files. 这意味着该类在2个或更多文件中传播。 So the Dispose is already defined in the form1.designer.cs. 所以Dispose已经在form1.designer.cs中定义。 So do not create another Dispose, or just remove the "partial" if you really must. 所以不要创建另一个Dispose,或者只是删除“部分”,如果你真的必须。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “&#39;Form1&#39; 已经定义了一个名为 &#39;.ctor&#39; 的成员,具有相同的参数类型” - "'Form1' already defines a member called '.ctor' with the same parameter types " 有 2 个错误,“开始”和“更新”错误 CS0111:类型“敌人”已经定义了一个名为“开始”的成员,具有相同的参数类型我该如何解决? - Got 2 errors with 'start' and 'update' error CS0111: Type 'Enemy' already defines a member called 'Start' with the same parameter types how can i fix? 错误2类型&#39;WindowsForms2012Snowman.WindowsForms2012SnowmanForm1&#39;已经定义了具有相同参数类型的名为&#39;Dispose&#39;的成员 - Error 2 Type 'WindowsForms2012Snowman.WindowsForms2012SnowmanForm1' already defines a member called 'Dispose' with the same parameter types 类型&#39;Startup&#39;已经定义了一个名为&#39;Configuration&#39;的成员,它具有相同的参数类型 - Type 'Startup' already defines a member called 'Configuration' with the same parameter types 错误已使用相同的参数类型定义了一个名为“索引”的成员 - Error already defines a member called 'Index' with the same parameter types 错误 - 已经使用相同的参数类型定义了一个名为“InitializeComponent”的成员 - Error - already defines a member called 'InitializeComponent' with the same parameter types C#错误:类型'x'已经定义了一个名为'y'的成员,它具有相同的参数类型 - C# error: Type 'x' already defines a member called 'y' with the same parameter types 错误1类型“ Pay”已经使用相同的参数类型定义了一个名为“ ComputePay”的成员 - Error 1 Type 'Pay' already defines a member called 'ComputePay' with the same parameter types C#:专用模板方法-错误:类型“…”已经定义了具有相同参数类型的成员“…” - C#: specialized template method - Error: Type '…' already defines a member called '…' with the same parameter types 错误 CS0111:“程序”类型已经定义了一个名为“Main”的成员,具有相同的参数类型 c# - error CS0111: Type 'Program' already defines a member called 'Main with the same parameter types c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM