c#表单继承事件处理程序触发两次

Nat*_*ath 0 c# inheritance button

我继承了一个有一个下一个和后一个按钮的根表单,然后在一些子表单上使用这些按钮,并获得两次调用的按钮,我相信这里将在MSDN中列出的问题:http: //msdn.microsoft.com /en-us/library/e33683a5(VS.71).aspx

我将如何应用他们给C#而不是VB的建议?

感谢,我的所有按钮都被调用两次会导致很多问题.

码:

根形式

public partial class rootForm : Form
    {
        public rootForm()
        {
            InitializeComponent();
        }

        private void rootForm_Load(object sender, EventArgs e)
        {
            //used for all generic strings e.g. buttons
            this.Text = Resources.UIStrings.FormTitle.ToString();
            this.btnNext.Text = Resources.UIStrings.btnNext.ToString();
            this.btnBack.Text = Resources.UIStrings.btnBack.ToString();
        }
Run Code Online (Sandbox Code Playgroud)

来自设计师:

        // btnNext
        // 
        this.btnNext.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        resources.ApplyResources(this.btnNext, "btnNext");
        this.btnNext.Name = "btnNext";
        this.btnNext.UseVisualStyleBackColor = true;
Run Code Online (Sandbox Code Playgroud)

安装表格

来自设计师

        // 
        // btnNext
        // 
        this.btnNext.Location = new System.Drawing.Point(373, 360);
        this.btnNext.Size = new System.Drawing.Size(99, 23);
        this.btnNext.TabIndex = 7;
        this.btnNext.Text = "Install";
        this.btnNext.Click += new System.EventHandler(this.installButton_Click);
Run Code Online (Sandbox Code Playgroud)

来自表格:

    private void installButton_Click(object sender, EventArgs e)
    {
     doSomeStuff();
    }
Run Code Online (Sandbox Code Playgroud)

从VS调用堆栈两次调用事件

Installer.exe!Installer.InstallerForm.installButton_Click(object sender = {Text = "Siguiente"}, System.EventArgs e = {X = 21 Y = 10 Button = Left}) Line 226    C#

    [External Code] 

Installer.exe!Installer.Program.Main() Line 21 + 0x1d bytes C#

[External Code] 
Run Code Online (Sandbox Code Playgroud)

Edit2 用断点跟踪callstack给出:

myInstaller.exe!myInstaller.InstallerForm.installButton_Click
[Native to Managed Transition]
[Managed to Native Transition]
System.Windows.Forms.dll!System.Windows.Forms.Control.OnClick
System.Windows.Forms.dll!System.Windows.Forms.Button.OnClick
System.Windows.Forms.dll!System.Windows.Forms.Button.OnMouseUp
System.Windows.Forms.dll!System.Windows.Forms.Control.WmMouseUp
System.Windows.Forms.dll!System.Windows.Forms.Control.WndProc
System.Windows.Forms.dll!System.Windows.Forms.ButtonBase.WndProc
System.Windows.Forms.dll!System.Windows.Forms.Button.WndProc
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.OnMessage
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.WndProc
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback
[Native to Managed Transition]
[Managed to Native Transition]
System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop
System.Windows.Forms.dll!System.Windows.Forms.Application.Run
myInstaller.exe!myInstaller.Program.Main
[Native to Managed Transition]
[Managed to Native Transition]
mscorlib.dll!System.AppDomain.ExecuteAssembly
Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context
mscorlib.dll!System.Threading.ExecutionContext.Run
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart
 ******************* Function: myInstaller.InstallerForm.installButton_Click(object, System.EventArgs), Thread: 0xE00 Main Thread

myInstaller.exe!myInstaller.InstallerForm.installButton_Click
[Native to Managed Transition]
[Managed to Native Transition]
System.Windows.Forms.dll!System.Windows.Forms.Control.OnClick
System.Windows.Forms.dll!System.Windows.Forms.Button.OnClick
System.Windows.Forms.dll!System.Windows.Forms.Button.OnMouseUp
System.Windows.Forms.dll!System.Windows.Forms.Control.WmMouseUp
System.Windows.Forms.dll!System.Windows.Forms.Control.WndProc
System.Windows.Forms.dll!System.Windows.Forms.ButtonBase.WndProc
System.Windows.Forms.dll!System.Windows.Forms.Button.WndProc
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.OnMessage
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.WndProc
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback
[Native to Managed Transition]
[Managed to Native Transition]
System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop
System.Windows.Forms.dll!System.Windows.Forms.Application.Run
myInstaller.exe!myInstaller.Program.Main
[Native to Managed Transition]
[Managed to Native Transition]
mscorlib.dll!System.AppDomain.ExecuteAssembly
Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context
mscorlib.dll!System.Threading.ExecutionContext.Run
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart
 ******************* Function: myInstaller.InstallerForm.installButton_Click(object, System.EventArgs), Thread: 0xE00 Main Thread
Run Code Online (Sandbox Code Playgroud)

如果我附加到添加处理程序的设计器部分,我得到

myInstaller.exe!myInstaller.InstallerForm.InitializeComponent
myInstaller.exe!myInstaller.InstallerForm.InstallerForm
myInstaller.exe!myInstaller.WelcomeForm.btnNext_Click
System.Windows.Forms.dll!System.Windows.Forms.Control.OnClick
System.Windows.Forms.dll!System.Windows.Forms.Button.OnClick
System.Windows.Forms.dll!System.Windows.Forms.Button.OnMouseUp
System.Windows.Forms.dll!System.Windows.Forms.Control.WmMouseUp
System.Windows.Forms.dll!System.Windows.Forms.Control.WndProc
System.Windows.Forms.dll!System.Windows.Forms.ButtonBase.WndProc
System.Windows.Forms.dll!System.Windows.Forms.Button.WndProc
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.OnMessage
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.WndProc
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback
[Native to Managed Transition]
[Managed to Native Transition]
System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop
System.Windows.Forms.dll!System.Windows.Forms.Application.Run
myInstaller.exe!myInstaller.Program.Main
[Native to Managed Transition]
[Managed to Native Transition]
mscorlib.dll!System.AppDomain.ExecuteAssembly
Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context
mscorlib.dll!System.Threading.ExecutionContext.Run
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart
 ******************* Function: myInstaller.InstallerForm.InitializeComponent(), Thread: 0xA34 Main Thread

myInstaller.exe!myInstaller.InstallerForm.InitializeComponent
myInstaller.exe!myInstaller.InstallerForm.performChecks
myInstaller.exe!myInstaller.InstallerForm.InstallerForm
myInstaller.exe!myInstaller.WelcomeForm.btnNext_Click
System.Windows.Forms.dll!System.Windows.Forms.Control.OnClick
System.Windows.Forms.dll!System.Windows.Forms.Button.OnClick
System.Windows.Forms.dll!System.Windows.Forms.Button.OnMouseUp
System.Windows.Forms.dll!System.Windows.Forms.Control.WmMouseUp
System.Windows.Forms.dll!System.Windows.Forms.Control.WndProc
System.Windows.Forms.dll!System.Windows.Forms.ButtonBase.WndProc
System.Windows.Forms.dll!System.Windows.Forms.Button.WndProc
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.OnMessage
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.WndProc
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback
[Native to Managed Transition]
[Managed to Native Transition]
System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop
System.Windows.Forms.dll!System.Windows.Forms.Application.Run
myInstaller.exe!myInstaller.Program.Main
[Native to Managed Transition]
[Managed to Native Transition]
mscorlib.dll!System.AppDomain.ExecuteAssembly
Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context
mscorlib.dll!System.Threading.ExecutionContext.Run
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart
 ******************* Function: myInstaller.InstallerForm.InitializeComponent(), Thread: 0xA34 Main Thread
Run Code Online (Sandbox Code Playgroud)

但这不应该是因为看起来表单被创建了两次?但如果我在installerform创建者上设置一个断点,它只被调用一次.

ser*_*hio 6

你可以添加一些代码,你如何继承和使用按钮?在C#中你不应该有这样的问题,因为没有Handles关键字.

但是,您可以在按钮事件中添加多个时间处理程序,例如:

parent
    myButton.Click += myClickAction

child
    sameButton.Click += sameClickAction
Run Code Online (Sandbox Code Playgroud)

继承时,不应单击设计器中的继承按钮(这将在继承的按钮上添加新的,可能相同的操作)

在两次执行的操作中添加一个breackpoint,当命中时请参考CallStack Visual Studio窗口,以查看谁调用您的方法两次...

编辑

不确定会发生什么,但请尝试以下方法:

更换:

this.btnNext.Click += new System.EventHandler(this.installButton_Click);
Run Code Online (Sandbox Code Playgroud)

this.btnNext.Click -= new System.EventHandler(this.installButton_Click);
this.btnNext.Click -= new System.EventHandler(this.installButton_Click);
this.btnNext.Click -= new System.EventHandler(this.installButton_Click);
this.btnNext.Click += new System.EventHandler(this.installButton_Click);
Run Code Online (Sandbox Code Playgroud)

EDIT2

为了分析对您方法的调用,请立即执行以下操作:

  1. 在installButton_Click方法中设置Tracepoint: alt text http://lh4.ggpht.com/_1TPOP7DzY1E/S13KtR6tpYI/AAAAAAAAC9s/GpCZpXEukv4/s800/3setBreackpoint.png

  2. 编辑tracepoint属性,以便跟踪callstack(或者只使用$ CALLER - 仅使用以前的函数): alt text http://lh6.ggpht.com/_1TPOP7DzY1E/S13KtE98okI/AAAAAAAAC9o/gW5TfXouQDM/s800/2brackpoint.png

  3. 在"输出"窗口中,取消激活(右键单击)除"程序输出"之外的所有消息;

  4. 在运行时分析您的输出:

alt text http://lh4.ggpht.com/_1TPOP7DzY1E/S13Ks9YHEHI/AAAAAAAAC9k/WgnO5cBtUxI/s800/1output.png