Ezi
Ezi

Reputation: 2210

Hooking up event handlers on a user control vb.net

I tried to use this code in the form:

AddHandler MyControl.MouseDown, AddressOf StartDrag

This wont give me an error, but it doesn't happen anything when I mouse down on the Control.

the same doesn't work if I put it in the user control.

    Private Sub StartDrag(ByVal sender As Object, ByVal e As MouseEventArgs)
    Dim Box = CType(sender, Control)
    Box.Tag = New DragInfo(Form.MousePosition, Box.Location)
    End Sub

Upvotes: 0

Views: 2091

Answers (2)

Jason Evans
Jason Evans

Reputation: 29186

I've just built a user control and added it to a form. Here is my code:

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            AddHandler UserControl1.MouseDown, AddressOf OnMouseDown
        End Sub

        Private Sub OnMouseDown (ByVal sender As Object, ByVal e As MouseEventArgs)
            MessageBox.Show("sdasd")
        End Sub
End Class

This works fine - the messagebox appears on the screen when I press the mouse button down. I have no code at all running in the user control, so no worries about having to call RaiseEvent.

Upvotes: 1

colithium
colithium

Reputation: 10327

Silly question but is that line of code ever executed? Does StartDrag look like this:

Private Sub StartDrag(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)

End Sub

Upvotes: 0

Related Questions