carstorm
carstorm

Reputation: 141

In Visual Basic how would I move the location of a form to that of the pointer when it is clicked?

I tried to use the code:

Private Sub SpaceInvadersControlButton_MouseDown (ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles SpaceInvadersControlButton.MouseDown
     If e.Button = Windows.Forms.MouseButtons.Right then
         Me.close
     ElseIf e.Button = Windows.Forms.MouseButtons.Left then
         Me.Location.X = MousePosition.X
         Me.Location.Y = MousePosition.Y
     End If
End Sub

However the two lines after the "ElseIf" statement give me this error:

Expression is a value and therefore cannot be the target of an assignment.

How would I do this without it erroring?

Upvotes: 0

Views: 1346

Answers (1)

Craig White
Craig White

Reputation: 14002

You cannot set the X and Y of a co-ordinate individually. Try setting them together;

Private Sub SpaceInvadersControlButton_MouseDown (ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles SpaceInvadersControlButton.MouseDown
     If e.Button = Windows.Forms.MouseButtons.Right then
         Me.close
     ElseIf e.Button = Windows.Forms.MouseButtons.Left then
         Me.Location = MousePosition
     End If
End Sub

Upvotes: 1

Related Questions