I want to after clicking on button2,after that if i click on button1 then msg is not displayed

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MsgBox("sonia")
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        RemoveHandler Button2.Click, AddressOf MYEVENTHandler
    End Sub

    Private Sub MYEVENTHandler(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub
End Class

Dani AI

Generated

The original RemoveHandler call is detaching the wrong thing. The intent is to stop the message shown by Button1 after Button2 is clicked, but the posted code removes a handler from Button2 instead of Button1. To detach the handler that shows the message, remove Button1's Click subscription at runtime:

RemoveHandler Button1.Click, AddressOf Button1_Click

Handlers wired by the designer with a Handles clause are attached during form initialization, but RemoveHandler still works if the exact same method reference is supplied. The important rule is delegate identity: the method passed to RemoveHandler must match the method that was subscribed. That explains why removing MYEVENTHandler (or removing from Button2) did not stop the message — it wasn't the subscription that was firing.

Notes and quick checklist:

  • Ensure the method passed to RemoveHandler is the same handler that was originally attached (same name and signature: sender As Object, e As EventArgs).
  • Put the RemoveHandler call where the detachment should occur (for this case, inside Button2's Click handler).
  • Add a debug trace or breakpoint to confirm the detach actually runs at runtime.
  • If runtime reattachment is needed later, re-subscribe using AddHandler (this is the dynamic pattern recommended and is useful when handlers must be toggled).

Both and point in the right direction: either remove the correct handler (as shown above) or switch to explicit AddHandler/RemoveHandler wiring so subscriptions are easy to manage.

Recommended Answers

All 2 Replies

I want to after clicking on button2,after that if i click on button1 then msg is not displayed

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MsgBox("sonia")
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        RemoveHandler Button2.Click, AddressOf MYEVENTHandler
    End Sub

    Private Sub MYEVENTHandler(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub
End Class

In your Form Load, setup the event handler for Button1. So, remove the statement Handles Button1.Click at the end of the Button1.Click procedure.

In your Form_Load place the following:
Addhandler Button1.Click, AddressOf MYEVENTHandler

In your MYEVENTHandler place the following code:
msgbox ("sonia")

In the Button2_Click, use the existing code you have, it will work.

Hope this helps.

you can use RemoveHandler statement for doing this

check this code:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.