Solved

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

Posted on 2016-09-01
6
64 Views
Last Modified: 2016-09-04
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

0
Comment
Question by:mainrotor
  • 2
  • 2
  • 2
6 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41779962
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
 

Author Comment

by:mainrotor
ID: 41780021
AndyAinscow,
That is not the cause.  

I commented out that line of code and still get the issue.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41780029
Are you clicking the button twice?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 32

Expert Comment

by:it_saige
ID: 41780158
Could you provide an example implementation that recreates the issue?

-saige-
0
 
LVL 32

Accepted Solution

by:
it_saige earned 500 total points
ID: 41780204
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
 

Author Comment

by:mainrotor
ID: 41784012
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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now