Solved

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

Posted on 2016-09-01
6
102 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

 
LVL 34

Expert Comment

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

-saige-
0
 
LVL 34

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

734 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