gpt4 book ai didi

c# - 我该如何修复错误 : Type Form 1' already defines a member called ' Dispose' with the same parameter types?

转载 作者:行者123 更新时间:2023-12-04 14:10:51 39 4
gpt4 key购买 nike

尝试更改方法名称也尝试创建新类并将 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)
{

}


}
}

错误在此行的 form1.designer.cs 中:
protected override void Dispose(bool disposing)

试图在此处和 form1 中更改方法名称,但没有奏效。

最佳答案

尼尔是对的,它是部分的,

如果您需要在处理时做一些工作,您也可以使用一些事件,例如 OnDispose、Dispose +=... 等。 dispose func 很奇怪。在设计器文件中自动生成。即使您更改设计器文件,它也会再次生成。因此您无法覆盖此重载方法。只是你可以这样称呼它。Dispose(true);...

如果您要处理其他东西,请使用它,

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

顺便说一句,我在删除designer.cs 方法后测试过不会导致Windows Forms 应用程序的重新生成。所以这将是可能的。以及。大多数情况下,当事情发生变化时,大多数代码生成器会替换设计器文件。比如EntityCode生成器,SL生成的代码。

关于c# - 我该如何修复错误 : Type Form 1' already defines a member called ' Dispose' with the same parameter types?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25661318/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com