Will
Will

Reputation: 1174

How to pass parameters in AddHandler using VB.NET

This is what I'm trying to do. I'm creating a dynamic check with an autoback that when clicked, will go to my subroutine and do something. The two parameters I'm trying to pass is the table which the checkox is located and the name of the id of the checkbox. But I'm getting the error

AddressOf must be the name of a method without parentheses or

method does not have a signature compatible with sender as object, e system.eventArgs". Here is my code below.

 chkSel = New CheckBox
 chkSel.ID = "check_" & CStr(a)              
 chkSel.AutoPostBack = True

  'This is where I get the error
  AddHandler chkSel.CheckedChanged, AddressOf change_operating_items(tableName, "check_" & CStr(a))  
 tblcell.Controls.Add(chkSel)
 tblrow.Cells.Add(tblcell)

Upvotes: 1

Views: 15720

Answers (2)

wjktrs
wjktrs

Reputation: 160

I had to use the following to send both the sender, e, and the custom parameters:

AddHandler chart1.PostPaint, Sub(sender, e) Chart1_BottomPostPaintScorePlot(sender, e, GroupNames) 

Private Sub Chart1_BottomPostPaintScorePlot(ByVal sender As Object, ByVal e As ChartPaintEventArgs, ByVal GroupNames() As String)

  If TypeOf e.ChartElement Is Legend Then

    'process the legend at the bottom 

  End If
End Sub

Upvotes: 2

Tim Schmelter
Tim Schmelter

Reputation: 460380

You cannot pass arguments when you register an event handler.

Instead you can pass them when you raise that event in case of a custom event.

Here you need to handle the CheckedChanged event, cast the Sender to CheckBox and use it's ID property.

Sub change_operating_items(sender As Object, e As EventArgs) 
    Dim chk = DirectCast(sender, CheckBox)
    Dim id = chk.ID
    ' do something with it '
EndSub 

Upvotes: 6

Related Questions