473,394 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Moving a form via a PictureBox

Hi !

I've got this strange problem :

When I press the left-button and move my mouse, my form moves just as
intended. But if I make big movements, the cursor shifts slightly. And
it ends with the mouse cursor completly out of the form, which is not
what I want, of course.

Here's the relevant snippet of my code :

private int lastX, lastY;

private void pictureBox_grip_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
this.lastX = MousePosition.X;
this.lastY = MousePosition.Y;
}

private void pictureBox_grip_MouseMove(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if(this.pictureBox_grip.Capture)
{
this.Location = new Point(this.Location.X + MousePosition.X -
this.lastX, this.Location.Y + MousePosition.Y - this.lastY);
this.lastX = MousePosition.X;
this.lastY = MousePosition.Y;
}
}

I also tried to use (e.X,e.Y) instead of MousePosition but the result
is worse : the form flickers a lot and the cursor shifts at once.

What am I doing wrong ?

Any help would be appreciated... It's driving me mad.

Nov 16 '05 #1
3 5727
Hi Robert,

maybe you should try it with:

private int mouseStartX, mouseStartY;
private int formStartX , formStartY;
private bool FormDragging = false;

private void pictureBox_grip_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
this.mouseStartX = MousePosition.X;
this.mouseStartY = MousePosition.Y;
this.formStartX = this.Location.X;
this.formStartY = this.Location.Y;
FormDragging = true;
}

private void pictureBox_grip_MouseMove(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if (FormDragging)
{
this.Location = new Point(
this.formStartX + MousePosition.X - this.mouseStartX,
this.formStartY + MousePosition.Y - this.mouseStartY
);
}
}

private void pictureBox_grip_MouseUp(object sender,
System.Windows.Forms.MouseEventArgs e)
{
FormDragging = false;
}

HTH,
Stefan
Robert Lochon schrieb:
Hi !

I've got this strange problem :

When I press the left-button and move my mouse, my form moves just as
intended. But if I make big movements, the cursor shifts slightly. And
it ends with the mouse cursor completly out of the form, which is not
what I want, of course.

Here's the relevant snippet of my code :

private int lastX, lastY;

private void pictureBox_grip_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
this.lastX = MousePosition.X;
this.lastY = MousePosition.Y;
}

private void pictureBox_grip_MouseMove(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if(this.pictureBox_grip.Capture)
{
this.Location = new Point(this.Location.X + MousePosition.X -
this.lastX, this.Location.Y + MousePosition.Y - this.lastY);
this.lastX = MousePosition.X;
this.lastY = MousePosition.Y;
}
}

I also tried to use (e.X,e.Y) instead of MousePosition but the result
is worse : the form flickers a lot and the cursor shifts at once.

What am I doing wrong ?

Any help would be appreciated... It's driving me mad.

Nov 16 '05 #2
Thanks a lot ! It works perfectly.

I understand that your solution is to compute the new position of the
form from its position when the mouse button was first pressed. Whereas
my solution was to compute its new position from its last position.
It's still not clear to me why my way was the wrong one... :o/

Nov 16 '05 #3
Hi Robert,

even though i'm not perfectly sure I can remember having the same
problem myself in another programming language.
The problem there was caused by multithreading, because new mouse events
were raised before the execution of the current one was finished
(redrawing a form is a pretty expensive process).
This would also explain why you encountered the problems only with fast
mouse movements and not with slow ones.

If you want you can try to secure your code by using locks or some other
form of semaphores.

HTH,
Stefan
Robert Lochon schrieb:
Thanks a lot ! It works perfectly.

I understand that your solution is to compute the new position of the
form from its position when the mouse button was first pressed. Whereas
my solution was to compute its new position from its last position.
It's still not clear to me why my way was the wrong one... :o/

Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Chris | last post by:
Hi, I am trying to set the position of a picture inside a picturebox with the folowing code: pctOutput.Picture = LoadPicture(App.Path & "\picture.bmp", , , , ) I didn't know what to fill...
1
by: Serdar C. | last post by:
hello i try to add this code to (mouse down) event of a picturebox1: picturebox1.location = new point(e.x,e.y) but i doesnt work, because the picture box sets its location to the mouse cursors...
1
by: Crirus | last post by:
Hi! I have a seamless image drawn onto a PictureBox. I wrote a mouseMove handle that allow me to drag the image around in that pictureBox. I whould like to do this When the border of the image...
10
by: Liz | last post by:
All: Can anyone tell me why my animated GIF does not show up as animated when put into a pictureBox?? Thanks. Liz
18
by: Ed Bitzer | last post by:
Can draw a line on my form with a button click event but cannot upon the Load event when I wish. No more line object so used the following: Dim bit As Bitmap = New Bitmap(Me.Width, Me.Height) Dim...
3
by: b747_440 | last post by:
Hello Newsgroup, I try to resize and move a picturebox. However, after each operation the picture box is being redrawn. This causes flickering. Is it possible to suppress the first redraw and draw...
8
by: Ryan | last post by:
Ok.. I have a form with lots of stuff on it; a tool strip panel, menu strip, data binding elements (dataset, binding source, table adapter), tab control with 7 tab pages, each page contains a...
2
by: toby | last post by:
Hi, After the great help I received to create PictureBox arrays I now have another problem. I want to be able to position transparent pictureboxes (with a small rectangle drawn in them) next...
2
by: pat.saunders | last post by:
Hi, I am using Visual Studio 2005 and am using Visual C#. I notice that the program.cs creates an instance of a form and runs it. I have a PictureBox in the form and a Class File x.cs and in the...
2
by: skynetplus | last post by:
Ok so far got the following: picBall.Left = picBall.Left - 10 picBall.Top = picBall.Top - 5 If picBall.Left >= Me.Width Then End If The top two lines allow the picturebox to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.