Question : Error with RemoveHandler and AddHandler

Hello,

In the following code I am trying to remove a handler before the procedure runs and add it back when the procedure concludes.  However I keep getting an error message stating that
lvSubprograms_ItemChecked does not have the correct signature in either the remove or add handler code
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
Private Sub cboTechnologySet_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboTechnologySet.SelectedIndexChanged

        Try
            RemoveHandler lvSubprograms.ItemCheck, AddressOf lvSubprograms_ItemChecked
            If cboTechnologySet.SelectedIndex > -1 Then
                Dim strSP As String = cboTechnologySet.SelectedItem.ToString
                TechnologyList = TechnologySetDB.GetProgramSubprogramList(strSP)
                lvSubprograms.Items.Clear()
                For j As Integer = 0 To TechnologyList.Count - 1
                    Dim t As New TechnologySet
                    t = TechnologyList(j)
                    lvSubprograms.Items.Add(t.TechnologySetCode)
                    lvSubprograms.Items(lvSubprograms.Items.Count - 1).SubItems.Add(t.SubTechnologyCode)
                    lvSubprograms.Items(lvSubprograms.Items.Count - 1).Checked = False
                Next
            Else
                MessageBox.Show("A selection must be made")
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, ex.GetType.ToString)
        Finally
            AddHandler lvSubprograms.ItemCheck, AddressOf lvSubprograms_ItemChecked
        End Try

    End Sub


    Private Sub lvSubprograms_ItemChecked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ItemCheckedEventArgs) Handles lvSubprograms.ItemChecked

        Dim mymetriclist As New List(Of Metrics)
        Try
            lvMetrics.Items.Clear()
            For Each checkedItem As ListViewItem In lvSubprograms.CheckedItems
                Dim m As New Metrics
                If cboInputCode.Text = "NEMS" Then
                    m.Technology = "NM"
                Else
                    m.Technology = checkedItem.SubItems(1).Text.ToString
                End If
                mymetriclist.Add(m)
            Next

            Dim myDataTable As New DataTable

            myDataTable = MetricsDB.GetMetricList(mymetriclist)
            'PopulateMetricsListView(myDataTable)

            Dim ds As New DataSet
            Dim dsHelper = New DataSetHelper(ds)
            ds.Tables.Add(myDataTable)
            Dim dtnew As New DataTable

            dtnew = dsHelper.SelectDistinct("DistinctMetrics", ds.Tables(0), "MetricName")


            'PopulateMetricsListView(dtnew)


        Catch ex As Exception
            MessageBox.Show(ex.Message, ex.GetType.ToString)
        End Try


    End Sub

Answer : Error with RemoveHandler and AddHandler

Hi chtullu135;

On the AddHandler / RemoveHandler the event needs to match the signature of the event you are calling. In your original code you had the the event lvSubprograms.ItemCheck which has a event handler with the signature :

Private Sub lvSubprograms_ItemChecked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ItemCheckedEventArgs) Handles lvSubprograms.ItemChecked

Note the event argument ItemCheckedEventArgs which differs from the event ItemCheck needs. The event ItemCheck needs to reference and event arg of ItemCheckEventArgs as shown in the next statement.

Private Sub lvSubprograms_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles lvSubprograms.ItemCheck

Fernando
Random Solutions  
 
programming4us programming4us