0

I have a windows form that has just one label on it. I need to be able to move the form around on the screen when it comes up. I tried the below code like suggested in many posts on google. But I could not make it working. Anyone has any idea what is going wrong here? Thanks for any help!

Public Class frmmsg
Inherits System.Windows.Forms.Form

'Windows Form Designer generated code here

Dim drag As Boolean
Dim mousex As Integer
Dim mousey As Integer

Private Sub frmmsg_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown, Me.MouseClick, label2.MouseClick, lblMessage.MouseClick
    If e.Button = Windows.Forms.MouseButtons.Left Then
        drag = True
        mousex = Windows.Forms.Cursor.Position.X - Me.Left
        mousey = Windows.Forms.Cursor.Position.Y - Me.Top
    End If
End Sub

Private Sub frmmsg_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove, label2.MouseMove, lblMessage.MouseMove
    If e.Button = Windows.Forms.MouseButtons.Left Then
        If drag Then
            Me.Top = Windows.Forms.Cursor.Position.Y - mousey
            Me.Left = Windows.Forms.Cursor.Position.X - mousex
        End If
    End If
End Sub

Private Sub frmmsg_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp, label2.MouseUp, lblMessage.MouseUp
    drag = False
End Sub

  Sub ShowMessage(ByVal psText$)

    If Not Me.Visible Then Me.Show()

    lblMessage.Text = psText
    Me.Refresh()

  End Sub

End Class

'This is how the above form is invoked from a different class

<NonSerialized()> Private mfrmmsg As frmmsg


If IsNothing(mfrmmsg) OrElse mfrmmsg.IsDisposed Then
        mfrmmsg = New frmmsg
End If

    mfrmmsg.ShowMessage(psText)
    mfrmmsg.TopMost = True
Jyina
  • 2,530
  • 9
  • 42
  • 80
  • explain "could not make it work" - thats how I have it coded (except for the showmessage thing) – Ňɏssa Pøngjǣrdenlarp Apr 23 '14 at 18:47
  • well, I attached the mouse events on the form to the appropriate mouse event handlers above. I did put break points in each of those event handlers and when I try to move the form when it comes up as a pop up, it does not move and the break points are not hit. Thanks. – Jyina Apr 23 '14 at 18:57

0 Answers0