9 Replies - 7468 Views - Last Post: 29 August 2009 - 07:23 AM Rate Topic: -----

#1 StealthTools  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 52
  • Joined: 11-March 08

How do you move picturebox with mouse?

Post icon  Posted 28 August 2009 - 10:32 PM

I have a form with a picturebox and I want to be able to drag the picturebox up and down with my mouse. I'm currently using a trackbar to move the picturebox up and down like this:

If TrackBar1.Value = 0 Then PictureBox7.Location = New Point(72, -165)
If TrackBar1.Value = 1 Then PictureBox7.Location = New Point(72, -185)
If TrackBar1.Value = 2 Then PictureBox7.Location = New Point(72, -195)
...


how would I use my mouse to do this?

This post has been edited by StealthTools: 28 August 2009 - 10:36 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How do you move picturebox with mouse?

#2 Luc001  Icon User is offline

  • D.I.C Addict

Reputation: 84
  • View blog
  • Posts: 617
  • Joined: 04-May 09

Re: How do you move picturebox with mouse?

Posted 28 August 2009 - 11:39 PM

Hi,

Here's a way how to move a picturebox with your mouse;

 Private Sub PictureBox_MouseMove(ByVal sender As Object, _
								 ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
		Static mousePosX As Single, mousePosY As Single
		If e.Button = 0 Then
			mousePosX = e.X
			mousePosY = e.Y
		Else
			PictureBox1.Left = PictureBox1.Left + (e.X - mousePosX)
			PictureBox1.Top = PictureBox1.Top + (e.Y - mousePosY)

		End If
	End Sub

Was This Post Helpful? 1

#3 StealthTools  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 52
  • Joined: 11-March 08

Re: How do you move picturebox with mouse?

Posted 29 August 2009 - 12:05 AM

View PostLuc001, on 28 Aug, 2009 - 10:39 PM, said:

Hi,

Here's a way how to move a picturebox with your mouse;

 Private Sub PictureBox_MouseMove(ByVal sender As Object, _
								 ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
		Static mousePosX As Single, mousePosY As Single
		If e.Button = 0 Then
			mousePosX = e.X
			mousePosY = e.Y
		Else
			PictureBox1.Left = PictureBox1.Left + (e.X - mousePosX)
			PictureBox1.Top = PictureBox1.Top + (e.Y - mousePosY)

		End If
	End Sub


This code works perfectly, I edited it just so I could move the picturebox up and down:

Public Class Form1
	Private Sub PictureBox_MouseMove(ByVal sender As Object, _
	ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
		Static mousePosY As Single
		If e.Button = 0 Then
			mousePosY = e.Y
		Else
			PictureBox1.Top = PictureBox1.Top + (e.Y - mousePosY)
		End If
	End Sub

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		Timer1.Start()
	End Sub

	Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
		TextBox1.Text = PictureBox1.Top
	End Sub
End Class



How though would I make the picturebox not be able to go too high or too low?
Was This Post Helpful? 0
  • +
  • -

#4 Luc001  Icon User is offline

  • D.I.C Addict

Reputation: 84
  • View blog
  • Posts: 617
  • Joined: 04-May 09

Re: How do you move picturebox with mouse?

Posted 29 August 2009 - 12:19 AM

Hi,

That can you do like this;

Private Sub PictureBox_MouseMove(ByVal sender As Object, _
								 ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
		Static mousePosY As Single
		Dim currenty As Single
		If e.Button = 0 Then
		   
			mousePosY = e.Y
		Else
			
			currenty = PictureBox1.Top + (e.Y - mousePosY)
			
			If Not (currenty < 0 Or currenty + PictureBox1.Height > Me.ClientSize.Height) Then
				PictureBox1.Top = currenty
			End If
		End If
	End Sub

Was This Post Helpful? 0
  • +
  • -

#5 StealthTools  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 52
  • Joined: 11-March 08

Re: How do you move picturebox with mouse?

Posted 29 August 2009 - 12:27 AM

View PostLuc001, on 28 Aug, 2009 - 11:19 PM, said:

Hi,

That can you do like this;

Private Sub PictureBox_MouseMove(ByVal sender As Object, _
								 ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
		Static mousePosY As Single
		Dim currenty As Single
		If e.Button = 0 Then
		   
			mousePosY = e.Y
		Else
			
			currenty = PictureBox1.Top + (e.Y - mousePosY)
			
			If Not (currenty < 0 Or currenty + PictureBox1.Height > Me.ClientSize.Height) Then
				PictureBox1.Top = currenty
			End If
		End If
	End Sub


That code keeps the picturebox within the bounds of the form, how could i set my own bounds for the picturebox?
Was This Post Helpful? 0
  • +
  • -

#6 Luc001  Icon User is offline

  • D.I.C Addict

Reputation: 84
  • View blog
  • Posts: 617
  • Joined: 04-May 09

Re: How do you move picturebox with mouse?

Posted 29 August 2009 - 12:34 AM

Hi,

Then you have to change this part of code and change the values to your proper values:

If Not (currenty < 10 Or currenty + PictureBox1.Height > 200) Then
				PictureBox1.Top = currenty
			End If

Was This Post Helpful? 0
  • +
  • -

#7 StealthTools  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 52
  • Joined: 11-March 08

Re: How do you move picturebox with mouse?

Posted 29 August 2009 - 12:49 AM

View PostLuc001, on 28 Aug, 2009 - 11:34 PM, said:

Hi,

Then you have to change this part of code and change the values to your proper values:

If Not (currenty < 10 Or currenty + PictureBox1.Height > 200) Then
				PictureBox1.Top = currenty
			End If



I'm having a hard time understanding the code.. :-|
Was This Post Helpful? 0
  • +
  • -

#8 Luc001  Icon User is offline

  • D.I.C Addict

Reputation: 84
  • View blog
  • Posts: 617
  • Joined: 04-May 09

Re: How do you move picturebox with mouse?

Posted 29 August 2009 - 01:05 AM

Hi,

Did yoy use the code and did you changed some values;

currenty < 10 to for example currenty < 50

and

PictureBox1.Height > 200 to for example Picturebox1.Height < 300

then you'll see that you can change the bounds of your picturebox.
Was This Post Helpful? 0
  • +
  • -

#9 StealthTools  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 52
  • Joined: 11-March 08

Re: How do you move picturebox with mouse?

Posted 29 August 2009 - 03:34 AM

View PostLuc001, on 29 Aug, 2009 - 12:05 AM, said:

Hi,

Did yoy use the code and did you changed some values;

currenty < 10 to for example currenty < 50

and

PictureBox1.Height > 200 to for example Picturebox1.Height < 300

then you'll see that you can change the bounds of your picturebox.


That works. Kinda hard for me to understand but changing the numbers around seems to do the job lol. Thanks :-)
Was This Post Helpful? 0
  • +
  • -

#10 Luc001  Icon User is offline

  • D.I.C Addict

Reputation: 84
  • View blog
  • Posts: 617
  • Joined: 04-May 09

Re: How do you move picturebox with mouse?

Posted 29 August 2009 - 07:23 AM

Hi,

No problem use this post was helpful, thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1