0

I've been trying to move a form that doesn't have a titlebar. I'm using a panel where the titlebar should be. This is as closest I've come to doing this. Once you look at my code you might laugh :)

Right now you can't move it with the canvas, but you can move it with a mousemove event. When I move it with the form, it drops down and to the right. Can someone PLEASE tell me where I'm going wrong. I'm guessing it's because there's no value to the a and b variabes in the MouseMove sub.

Private Sub forml_MouseDown(sender As Object, e As MouseEventArgs) Handles Panel.MouseDown
    Dim newPoint As New System.Drawing.Point()
    Dim a As Integer
    Dim b As Integer

    a = Panel.MousePosition.X - Me.Location.X
    b = Panel.MousePosition.Y - Me.Location.Y
End Sub

Private Sub form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
    Dim newPoint As New System.Drawing.Point()

    Dim a As Integer
    Dim b As Integer

    If e.Button = MouseButtons.Left Then
        newPoint = Panel.MousePosition
        newPoint.X = newPoint.X - (a)
        newPoint.Y = newPoint.Y - (b)
        Me.Location = newPoint
    End If
End Sub

I would REALLY appreciate some help.

jumper
  • 91
  • 1
  • 15
  • The *code option* is very easy. Paste in the code, select it, and hit Ctrl+K on your keyboard or the toolbar button with the `{}` image. – Ken White Sep 02 '15 at 17:28
  • See [How to move form without form border (visual studio)](http://stackoverflow.com/a/24449733/719186) – LarsTech Sep 02 '15 at 17:34
  • It is just buggy. Your a and b variable must be members of the form class, not local variables. Your MouseMove event handler listens to the form, not the panel. – Hans Passant Sep 02 '15 at 17:57
  • Thanks man... really apprecaite it... – jumper Sep 02 '15 at 18:08
  • I tried some of the other code from the link and they don't work for me. I'm using VS2015. Maybe that has a little to do with it. I know it used to be real easy. I did get it to focus on the panel so now I got that part. just need to know what is causing that jerk sideways when I first move the form. – jumper Sep 02 '15 at 18:46

1 Answers1

0

I got it to work! You got it Hans.. I made a and b members of the class and I handled the panel thing by the code you'll see below. Thanks to you both for all the help.

:this works

 Private Sub planel_MouseDown(sender As Object, e As MouseEventArgs) Handles Panel.MouseDown

        Dim newPoint As New System.Drawing.Point()

        a = Panel.MousePosition.X - Me.Location.X
        b = Panel.MousePosition.Y - Me.Location.Y

    End Sub
    Private Sub panel_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel.MouseMove

        Dim newPoint As New System.Drawing.Point()

        If e.Button = MouseButtons.Left Then

            newPoint = Panel.MousePosition
            newPoint.X = newPoint.X - (a)
            newPoint.Y = newPoint.Y - (b)
            Me.Location = newPoint
        End If
    End Sub
jumper
  • 91
  • 1
  • 15