Hi,

I have a User Control that contains a Button. This button works for the first click. But the Click Event is not fired in the second click. Please help.

Here's my code:

Public Sub New()
        MyBase.New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Dim doc As Document = Application.DocumentManager.MdiActiveDocument
        AddHandler doc.CommandEnded, AddressOf Me.plineCommandEnded
    End Sub

Private Sub ucTools_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
        Dim doc As Document = Application.DocumentManager.MdiActiveDocument
        RemoveHandler doc.CommandEnded, AddressOf Me.plineCommandEnded
    End Sub

Private Sub btnSpace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSpace.Click
        InitializeComponent()
        Dim doc As Document = Application.DocumentManager.MdiActiveDocument
        CmdStarted = True
        doc.SendStringToExecute("_PLINE" & vbCr, True, False, True)
End Sub

Thanks

Recommended Answers

All 3 Replies

Using the InitializeComponent() procedure is the problem. Instead of using this method, you should create a custom method that will "reset" all your controls. On this way you are not overwriting the handlers of your controls event.

If you use InitializeComponent() you actually create a new button for which a handler does not exist in your code. Even though there is a " Handles Button1.Click" in your code, this code will not apply to this button, because the button got created AFTER the eventhandlers got added to your form.

Hope i could explain it good enough.

Great!! Thanks very much! I commented out the InitializeComponent() and everything works fine. Thanks again.

Don't forget to mark this thread as solved if you found your solution! ;)

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.