I have a event and have many handles. How can i remove all handles but dont know exactly name of any handle/. :cool:
Printable View
I have a event and have many handles. How can i remove all handles but dont know exactly name of any handle/. :cool:
As far as I'm aware, you can't. If you're adding handlers then you know what you're adding, so you should know what you need to remove. It's up to you to write good code, which this is part of. In general, the code module that does something is responsible for undoing it, e.g. creating and destroying an object or adding and removing a handler.
That is code i add handle to ToolStripItemclick_Click event.Code:Private Sub dtgcapnhatthongtin_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dtgcapnhatthongtin.KeyDown
If cobcapnhatthongtin.SelectedIndex = 2 Then
Dim dt As DataTable = dat.GetDanhSachMonAn
ContextMenuStrip1.Items.Clear()
Dim tollnhande As New ToolStripMenuItem
tollnhande.Text = "Lựa chọn thực đơn :"
tollnhande.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
tollnhande.ForeColor = System.Drawing.SystemColors.Desktop
ContextMenuStrip1.Items.Add(tollnhande)
For i As Integer = 0 To dt.Rows.Count - 1
If dt.Rows(i)(2).ToString.Substring(0, 1) = e.KeyCode.ToString Then
Dim toll As New ToolStripMenuItem
toll.Text = dt.Rows(i)(2).ToString
AddHandler toll.Click, AddressOf ToolStripItemclick_Click
ContextMenuStrip1.Items.Add(toll)
End If
Next
If ContextMenuStrip1.Items.Count > 1 Then
Dim rec As Rectangle
rec = dtgcapnhatthongtin.GetCellDisplayRectangle(2, 1, True)
ContextMenuStrip1.Show(dtgcapnhatthongtin, New Point(rec.X, rec.Y))
End If
End If
End Sub
Private Sub ToolStripItemclick_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Try
If cobcapnhatthongtin.SelectedIndex = 2 Then
For i As Integer = 0 To dtgcapnhatthongtin.RowCount - 1
If dtgcapnhatthongtin.Rows(i).Cells(2).Value = DirectCast(sender, ToolStripMenuItem).Text Then
dtgcapnhatthongtin.CurrentCell = dtgcapnhatthongtin(0, i)
Exit Sub
End If
Next
End If
Catch ex As Exception
MessageBoxEx.Show("Đã xảy ra sự cố : " & ex.Message, "Thông báo sự cố", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
End Try
End Sub
How can i remove all handles of that event.
I am goting to named for each toolstripitem before add handle. :wave:
It's not like you're adding multiple handlers for the same event. You're simply adding one handler to the Click event of each item in ContextMenuStrip1. As such, all you need to do is loop through those items and remove a handler from each one.
Well, if you know about AddHandler, then surely you know about RemoveHandler, right?
-tg