Subject: Button Handler Being called multiple times
Posted By: ExDb Post Date: 1/22/2006 6:28:32 PM
Hello folks - here's another question for you:

I have a Class (class1), that inherits a Windows.Form.  On a button click, it passes a reference of itself to another class (class2).  The aim is to handle all of the form control events in class2, thus freeing up coding space in class1.

Class 2 looks like this:

Public Class Class2

   Dim cl1 As Class1
   Dim iCode As Integer

   Public SetData(ByVal iCode As Integer, ByRef cl1 As Class1)

      Me.iCode = iCode
      Me.cl1 = Class1

      'Add Button Handlers
      AddHandler Class1.btn1.Click, AddressOf btn1_Click
      AddHandler Class1.btn2.Click, AddressOf btn2_Click
      AddHandler Class1.btn3.Click, AddressOf btn3_Click
      AddHandler Class1.btn4.Click, AddressOf btn4_Click

      .......

   End Sub

   Private Sub btn1_Click
      MsgBox("btn1_Click")
   End Sub

   Private Sub btn2_Click
      MsgBox("btn2_Click")
   End Sub

   Private Sub btn3_Click
      MsgBox("btn3_Click")
   End Sub

   Private Sub btn4_Click
      MsgBox("btn4_Click")
   End Sub

End Class

There are 3 buttons in Class1 that create a new instance of Class2 and call the SetData passing a different value for iCode but the same created instance of Class1.  The problem I have is you click btn3 for example, after the message box is displayed, it's handler is automatically called again, sometimes up to 4 times without me doing anything!  I've experimented pressing the 4 buttons in Class1 and pressing any of the buttons handled in Class2 and sometimes the associated message box is displayed just the once (which is what should be happening), and on other occassions it can be displayed multiple times.

Can someone elighten me as to what's wrong with my code?

Thanks in advance

Reply By: ExDb Reply Date: 1/22/2006 6:32:43 PM
Before someone replies, my silly mistake was to not add in the example the handler signatures (ByVal sender As Object, ByVal e As System.EventArgs) for each button handler.  Take it as read that I have done this in the class.


Go to topic 38988

Return to index page 388
Return to index page 387
Return to index page 386
Return to index page 385
Return to index page 384
Return to index page 383
Return to index page 382
Return to index page 381
Return to index page 380
Return to index page 379