Solved

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

Posted on 2016-09-01
6
115 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month11 days, 1 hour left to enroll

632 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