|
-
Jan 8th, 2010, 08:41 AM
#1
Thread Starter
Fanatic Member
How to remove all handle in a event
I have a event and have many handles. How can i remove all handles but dont know exactly name of any handle/.
-
Jan 8th, 2010, 08:53 AM
#2
Re: How to remove all handle in a event
 Originally Posted by manhit45
I have a event and have many handles. How can i remove all handles but dont know exactly name of any handle/. 
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.
-
Jan 8th, 2010, 09:13 AM
#3
Thread Starter
Fanatic Member
Re: How to remove all handle in a 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
That is code i add handle to ToolStripItemclick_Click event.
How can i remove all handles of that event.
I am goting to named for each toolstripitem before add handle.
-
Jan 8th, 2010, 09:24 AM
#4
Re: How to remove all handle in a event
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.
-
Jan 8th, 2010, 09:25 AM
#5
Re: How to remove all handle in a event
Well, if you know about AddHandler, then surely you know about RemoveHandler, right?
-tg
-
Jan 8th, 2010, 09:43 AM
#6
Thread Starter
Fanatic Member
Re: How to remove all handle in a event
 Originally Posted by jmcilhinney
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.
Sorry, yes.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|