I've been looking for a little while for some code that would allow the user to 'click and drag' to move around a borderless form. I have achieved this in VB.Net and C# in Windows Forms, and have I believe, done it in Excel historically (although I cannot remember the code). I cannot seem to work out the translations into Access VBA, primarily because the 'left' method cannot be applied to a Form object in a Private Sub (I think?):
Me.Left
Without this, I'm struggling to translate the code, so is there another way, perhaps with Windows API calls or just Form events to make this happen? I'd really like to exhaust the possibilities as Borderless Forms look so nice!
Any help hugely appreciated.
Here is the VB.Net version that works:
Dim dragForm As Boolean
Dim xDrag As Integer
Dim yDrag As Integer
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
dragForm = True
xDrag = Windows.Forms.Cursor.Position.X - Me.Left
yDrag = Windows.Forms.Cursor.Position.Y - Me.Top
End Sub
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
If dragForm Then
Me.Top = Windows.Forms.Cursor.Position.Y - yDrag
Me.Left = Windows.Forms.Cursor.Position.X - xDrag
End If
End Sub
Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
dragForm = False
End Sub
Here is my attempt to re-write this so far:
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim xx As Long
Dim yy As Long
xx = Me.Left + X - xDrag
yy = Me.Top + Y - yDrag
Me.Left = xx
Me.Top = yy
moveFrm = False
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim xx As Long
Dim yy As Long
If moveFrm = True Then
xx = Me.Left + X - xDrag
yy = Me.Top + Y - yDrag
Me.Left = xx
Me.Top = yy
End If
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
moveFrm = True
xDrag = X
yDrag = Y
End Sub
Form.Top
andForm.Left
in Access I can answer that. But I won't go creating a VB.Net application with the provided code, creating an Access application, and testing if they behave the same with some adjustments. - Erik A