Results 1 to 6 of 6

Thread: How to remove all handle in a event

  1. #1

    Thread Starter
    Fanatic Member manhit45's Avatar
    Join Date
    May 2009
    Location
    Ha noi - Viet Nam
    Posts
    826

    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/.
    --***----------***-----

    If i help you please rate me.

    Working with Excel * Working with String * Working with Database * Working with array *

    K51 ĐH BÁCH KHOA HÀ NỘI - Khoa CNTT pro.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: How to remove all handle in a event

    Quote Originally Posted by manhit45 View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member manhit45's Avatar
    Join Date
    May 2009
    Location
    Ha noi - Viet Nam
    Posts
    826

    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.
    --***----------***-----

    If i help you please rate me.

    Working with Excel * Working with String * Working with Database * Working with array *

    K51 ĐH BÁCH KHOA HÀ NỘI - Khoa CNTT pro.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: How to remove all handle in a event

    Well, if you know about AddHandler, then surely you know about RemoveHandler, right?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    Fanatic Member manhit45's Avatar
    Join Date
    May 2009
    Location
    Ha noi - Viet Nam
    Posts
    826

    Re: How to remove all handle in a event

    Quote Originally Posted by jmcilhinney View Post
    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.
    --***----------***-----

    If i help you please rate me.

    Working with Excel * Working with String * Working with Database * Working with array *

    K51 ĐH BÁCH KHOA HÀ NỘI - Khoa CNTT pro.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width