Visualbasic FormAnwendung; mouse/Picturebox location

07/15/2020 07:28 ProDielerNR(1)#1
Keine Ahnung wie ich den titel nennen sollte
aber mein Problem ist es das ich es nicht hinbekommen
wen ich mit meiner mouse auf der Picturebox bin und linke mouse geklickt halte
das die picturebox die Koordinaten von der mouse fährt wichtig hier bei ist das es um die links rechts Koordinaten geht

mein versuch wahr es daher das mouse move event wen mouse auf der Picturebox ist.

Picturebox1.location = (e.X,100) oder Picturebox1.Location (mouseposition. X,100)
wahren so meine ideen, aber beides hat nicht funktioniert ^^"
darauf folgten dann eha hilflose versuche das ich gesagt habe
das wen mouse auf der picbox ist das es ein integer 1 geben soll
und wen es leave 0
und hab dann das Mousemove event einzeln genommen und hab
abgefragt darin ist integer 1 + mouse left click dann soll
Picturebox1.location = (e.X,100) passieren ^^"
hat auch nicht geklappt

Wüsstet ihr wie ich es machen könnte ? ^^"
07/15/2020 15:51 Legendary#2
Du kannst für die PictureBox ein Event registrieren (MouseDown). Im Constructor des Events (im Code) gibt es 2x Parameter. 1x den Sender und die EventArgs.

Code:
Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles MyBase.MouseDown
    MessageBox.Show(e.X & ", " & e.Y)
End Sub
Bei den Event-Args hast du die "X" Koordinate, welche für die links/rechts Position zuständig ist. Hoffe das hilft dir etwas weiter.
07/15/2020 20:37 ProDielerNR(1)#3
hat mir leider nicht geholfen :?

um es spezifischer zu beschreiben, ich möchte mit der mouse die pickbox hin und her verschieben auf einer bestimmten x axe (kordinaten)
07/16/2020 18:31 elmarcia#4
Quote:
Originally Posted by ProDielerNR(1) View Post
hat mir leider nicht geholfen :?

um es spezifischer zu beschreiben, ich möchte mit der mouse die pickbox hin und her verschieben auf einer bestimmten x axe (kordinaten)
I don't know much vb sintax, but here is the basic algorithm:

1- Register mouse down event either for form or picture itself
1.a- Inside event handler save click location in a global or accesible variable in your code named $prevMousePosition, set $movingX and $movingY variables to false or sth like that, and set $mouseDown to true

2-Register mouse move event in form
2.a - if(not $mouseDown) return; //exit function nothing to do here
2.b - Compute $prevMousePosition vs $currentMouse if $movingX or $movingY are false, something like this:

if(not movingX and not movingY){
dx = $currentMouse.X - $prevMousePosition.X
dy = $currentMouse.Y - $prevMousePosition.Y
if(dx > dy)
$movingX = true
else
$movingY = true
}
if($movingX){
//lock x axis to click location in Y
Picturebox1.location = ($currentMouse.X,$prevMousePosition.Y)
}else if($movingY){
Picturebox1.location = ($prevMousePosition.X,$currentMouse.Y)
}


3- Register mouse up event: clear $mouseDown flag and set it to false

Reference for mouse events
[Only registered and activated users can see links. Click Here To Register...]

Notes: words with $ means variables, just to clarify.

Summary:
Code:
mouseDownEvent(_,event){
movingX = false;
movingY = false;
prevMousePosition = event;
mouseDown = true;
}

mouseUpEvent(_,event){
mouseDown = false;
}

mouseMoveEvent(_,event){
if(not mouseDown) return;

f(not movingX and not movingY){
  dx = event.X - prevMousePosition.X
  dy = event.Y - prevMousePosition.Y
  if(dx > dy) 
  movingX = true
  else 
 movingY = true
}
if(movingX){
//lock x axis to click location in Y
Picturebox1.location = (event.X,prevMousePosition.Y)
}else if($movingY){
Picturebox1.location = (prevMousePosition.X,event.Y)
}

}
You can expand this code saving the object that you clicked in a variable and updating location of saved object
07/17/2020 00:59 ProDielerNR(1)#5
I've already tried this in my code language (VB.net Application form) but it didn't work :)

Code:
Imports System
Imports System.Drawing
Imports System.Windows.Forms

Public Class Form1
    Private Sub PictureBox1_MouseEnter(sender As Object, e As EventArgs) Handles PictureBox1.MouseEnter
        PictureBox1.Location = New Point(MousePosition.X, 100)
    End Sub
End Class
This is my first try for this
but have not actually work

Code:
Imports System
Imports System.Drawing
Imports System.Windows.Forms

Public Class Form1
    Dim DragFlag As Boolean
    Dim MemX As Integer, MemY As Integer

    Private Sub picturebox1_MouseDown1(ByVal sender As Object,
    ByVal e As System.Windows.Forms.MouseEventArgs) _
    Handles PictureBox1.MouseDown
        DragFlag = True : MemX = e.X : MemY = e.Y
    End Sub

    Private Sub picturebox1_MouseMove1(ByVal sender As Object,
        ByVal e As System.Windows.Forms.MouseEventArgs) _
        Handles PictureBox1.MouseMove
        If Not DragFlag Then Exit Sub
        PictureBox1.Left = PictureBox1.Left + (e.X - MemX)
       
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        PictureBox1.Location = New Point(50, 100)
    End Sub

    Private Sub Picture2_MouseUp1(ByVal sender As Object,
    ByVal e As System.Windows.Forms.MouseEventArgs) _
    Handles PictureBox1.MouseUp
        DragFlag = False
    End Sub
End Class
I have found my solution! :D
Closed Quest