在用户控件vb.net上挂起事件处理程序

时间:2011-07-27 20:43:46

标签: .net vb.net user-controls

我尝试以下列形式使用此代码:

AddHandler MyControl.MouseDown, AddressOf StartDrag

这不会给我一个错误,但是当我在控件上按下鼠标时,它不会发生任何事情。

如果我把它放在用户控件中,它就不起作用。

    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

2 个答案:

答案 0 :(得分:1)

我刚刚构建了一个用户控件并将其添加到表单中。这是我的代码:

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

这很好用 - 当我按下鼠标按钮时,屏幕上会出现消息框。我没有在用户控件中运行的代码,因此不必担心必须调用RaiseEvent。

答案 1 :(得分:0)

愚蠢的问题,但这条代码行是否曾执行过? StartDrag看起来像这样:

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

End Sub
相关问题