I Need help preventing my button click event from being raised twice on my VB.Net application

Hi Experts,
I need help preventing my button click event from being raised twice on my VB.Net application.

I use a base class and an interface on my VB.Net application.

I override the button_click event on my base class, so that the button_click event fires on my interface.  However, by doing this the button_click event on my interface executes twice.  I need help preventing this from happening.  How can I do this?  

I have included the button_click event of my base and interface classes below.

Base Class button_click event
overidable Sub btnPrint_Click(ByVal Sender AS System.object, ByVal e As system.EventArgs) Handles btnPrint.click
'...some code goes here
End Sub

Open in new window


interface class button_click event
This is the code that fires twice.
overrides Sub btnPrint_Click(Sender As object, e as Event Args) Handles btnPrint.click
   If (X = 0) Then
          MsgBox("Test")
   Else
          MyBase.btnPrint_Click(sender, e)
   End If
End Sub

Open in new window

mainrotorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
overrides Sub btnPrint_Click(Sender As object, e as Event Args) Handles btnPrint.click
   If (X = 0) Then
          MsgBox("Test")
   Else
          MyBase.btnPrint_Click(sender, e)    <=====  Is this the cause?
   End If
End Sub
0
mainrotorAuthor Commented:
AndyAinscow,
That is not the cause.  

I commented out that line of code and still get the issue.
0
AndyAinscowFreelance programmer / ConsultantCommented:
Are you clicking the button twice?
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

it_saigeDeveloperCommented:
Could you provide an example implementation that recreates the issue?

-saige-
0
it_saigeDeveloperCommented:
Nevermind, I believe I recreated your issue:

Form1.vb -
Public Class Form1

	Private Sub OnClick(sender As Object, e As EventArgs) Handles Button1.Click
		Using child As New ChildForm()
			child.ShowDialog(Me)
		End Using
	End Sub
End Class

Open in new window

Form1.Designer.vb -
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
		Me.Button1 = New System.Windows.Forms.Button()
		Me.RichTextBox1 = New System.Windows.Forms.RichTextBox()
		Me.SuspendLayout()
		'
		'Button1
		'
		Me.Button1.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
		Me.Button1.Location = New System.Drawing.Point(12, 12)
		Me.Button1.Name = "Button1"
		Me.Button1.Size = New System.Drawing.Size(260, 23)
		Me.Button1.TabIndex = 1
		Me.Button1.Text = "LAUNCH CHILD!!!!"
		Me.Button1.UseVisualStyleBackColor = True
		'
		'RichTextBox1
		'
		Me.RichTextBox1.Location = New System.Drawing.Point(13, 42)
		Me.RichTextBox1.Name = "RichTextBox1"
		Me.RichTextBox1.Size = New System.Drawing.Size(259, 207)
		Me.RichTextBox1.TabIndex = 2
		Me.RichTextBox1.Text = ""
		'
		'Form1
		'
		Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
		Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
		Me.ClientSize = New System.Drawing.Size(284, 261)
		Me.Controls.Add(Me.RichTextBox1)
		Me.Controls.Add(Me.Button1)
		Me.Name = "Form1"
		Me.Text = "Form1"
		Me.ResumeLayout(False)

	End Sub
	Friend WithEvents Button1 As System.Windows.Forms.Button
	Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox

End Class

Open in new window

BaseForm.vb -
Public Class BaseForm
	Protected Overridable Sub OnClick(sender As Object, e As EventArgs) Handles Button1.Click
		If (Owner IsNot Nothing) Then
			If TypeOf Owner Is Form1 Then
				Dim frm = DirectCast(Owner, Form1)
				frm.RichTextBox1.AppendText(String.Format("BaseForm calling from Overridable OnClick{0}", Environment.NewLine))
			End If
		End If
	End Sub
End Class

Open in new window

BaseForm.Designer.vb -
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class BaseForm
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
		Me.Button1 = New System.Windows.Forms.Button()
		Me.SuspendLayout()
		'
		'Button1
		'
		Me.Button1.Font = New System.Drawing.Font("Microsoft Sans Serif", 24.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
		Me.Button1.Location = New System.Drawing.Point(55, 68)
		Me.Button1.Name = "Button1"
		Me.Button1.Size = New System.Drawing.Size(175, 125)
		Me.Button1.TabIndex = 1
		Me.Button1.Text = "PRESS ME!!!!"
		Me.Button1.UseVisualStyleBackColor = True
		'
		'BaseForm
		'
		Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
		Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
		Me.ClientSize = New System.Drawing.Size(284, 261)
		Me.Controls.Add(Me.Button1)
		Me.Name = "BaseForm"
		Me.Text = "BaseForm"
		Me.ResumeLayout(False)

	End Sub
	Friend WithEvents Button1 As System.Windows.Forms.Button
End Class

Open in new window

ChildForm.vb -
Public Class ChildForm
	Protected Overrides Sub OnClick(sender As Object, e As EventArgs) Handles Button1.Click
		If (Owner IsNot Nothing) Then
			If TypeOf Owner Is Form1 Then
				Dim frm = DirectCast(Owner, Form1)
				frm.RichTextBox1.AppendText(String.Format("ChildForm calling from Overrides OnClick{0}", Environment.NewLine))
			End If
		End If
		MyBase.OnClick(sender, e)
	End Sub
End Class

Open in new window

ChildForm.Designer.vb -
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class ChildForm
	Inherits BaseForm

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        components = New System.ComponentModel.Container
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.Text = "Child"
    End Sub
End Class

Open in new window

Produces the following output -

Initial load -Capture.JPGPressing 'Launch Child' button -Capture.JPGPressing the 'Press Me' button and closing child -Capture.JPG
To correct, simply remove the 'Handles Button1.Click' in the child (you don't need to rehandle the event).  Corrected ChildForm.vb -
Public Class ChildForm
	Protected Overrides Sub OnClick(sender As Object, e As EventArgs)
		If (Owner IsNot Nothing) Then
			If TypeOf Owner Is Form1 Then
				Dim frm = DirectCast(Owner, Form1)
				frm.RichTextBox1.AppendText(String.Format("ChildForm calling from Overrides OnClick{0}", Environment.NewLine))
			End If
		End If
		MyBase.OnClick(sender, e)
	End Sub
End Class

Open in new window


Now produces -Capture.JPG
-saige-
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mainrotorAuthor Commented:
it_saige,
Wow!  That was pretty cool, how you wrote all that code to recreate the event.

Thank you very much for the solution.

mrotor.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.