English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Public Class Form1
Dim x, y As Integer
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If (e.Button = Windows.Forms.MouseButtons.Left) Then
x = e.X
y = e.Y
PictureBox1.Location = New Point(x, y)
End If
End Sub
End Class

2007-06-02 06:26:00 · 2 answers · asked by fred12ned 2 in Computers & Internet Programming & Design

Why Does It Vibrate??

2007-06-02 08:00:57 · update #1

2 answers

You are using the MouseMove event to move the same object that is triggering the event. The rapid movement you see is from rapid triggering of the event as the picturebox is moved.

To rectify this use two Pictureboxes One is normally visible and the other is hidden. When you want to move PictureBox1 you will hide Picturebox1 and show PictureBox2. As you drag you are really moving Picturebox2 so the user has something to see and you are not retriggering the Picturebox1 event until the mose actually moves the object.

To pull this swap off you need to use the mouse down and mouse up events to switch the Pictureboxes (1&2) visible properties and Move Picturebox 2 into the sameposition as Picturebox1

Next Issue you have is thatthe X & Y coordinates of the Picturebox mouse move are in relation to the picturbox origin and NOT its actual location on the form

You will need to capture the picturebox original location on MouseDown and add the Original loactions as offsets to the e.x & e.Y

I have writtien some code using two Pictureboxes. Place two pictureboxes on your for with Picturebox2 visible property set to false. To aid in the visualization change the background colors. I set PictureBox1 to RED and PictureBox2 to Blue

Dim x, y, xOrg, yOrg As Integer



Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
PictureBox2.Location = PictureBox1.Location
xOrg = PictureBox1.Left
yOrg = PictureBox1.Top
PictureBox1.Visible = False
PictureBox2.Visible = True

End Sub

Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
PictureBox1.Location = PictureBox2.Location
PictureBox1.Visible = True
PictureBox2.Visible = False
End Sub



Private Sub PictureBox1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove

If (e.Button = Windows.Forms.MouseButtons.Left) Then
Debug.WriteLine(e.Y)
x = e.X + xOrg
y = e.Y + yOrg
PictureBox2.Location = New Point(x, y)
End If
End Sub

2007-06-02 12:42:48 · answer #1 · answered by MarkG 7 · 0 0

I have tried your code after adding PictureBox1 to my form abd verified its working, its working fine and not showing any errors, but while draging its showing some sort of vibrations
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If (e.Button = System.Windows.Forms.MouseButtons.Left) Then
x = e.X
y = e.Y
PictureBox1.Location = New Point(x, y)
End If
End Sub

2007-06-02 14:19:09 · answer #2 · answered by dohub_dohub 2 · 0 0

fedest.com, questions and answers