473,625 Members | 3,201 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(obje ct sender,
System.Windows. Forms.MouseEven tArgs e)
{
this.lastX = MousePosition.X ;
this.lastY = MousePosition.Y ;
}

private void pictureBox_grip _MouseMove(obje ct sender,
System.Windows. Forms.MouseEven tArgs e)
{
if(this.picture Box_grip.Captur e)
{
this.Location = new Point(this.Loca tion.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 5738
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(obje ct sender,
System.Windows. Forms.MouseEven tArgs e)
{
this.mouseStart X = MousePosition.X ;
this.mouseStart Y = MousePosition.Y ;
this.formStartX = this.Location.X ;
this.formStartY = this.Location.Y ;
FormDragging = true;
}

private void pictureBox_grip _MouseMove(obje ct sender,
System.Windows. Forms.MouseEven tArgs e)
{
if (FormDragging)
{
this.Location = new Point(
this.formStartX + MousePosition.X - this.mouseStart X,
this.formStartY + MousePosition.Y - this.mouseStart Y
);
}
}

private void pictureBox_grip _MouseUp(object sender,
System.Windows. Forms.MouseEven tArgs 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(obje ct sender,
System.Windows. Forms.MouseEven tArgs e)
{
this.lastX = MousePosition.X ;
this.lastY = MousePosition.Y ;
}

private void pictureBox_grip _MouseMove(obje ct sender,
System.Windows. Forms.MouseEven tArgs e)
{
if(this.picture Box_grip.Captur e)
{
this.Location = new Point(this.Loca tion.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
15504
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 in at and so the result was:
1
1466
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 position on the screen, not the position inside the form... how can ido that? how can i move the picturebox in a form by holding down the left mouse button and move it and then after release the mouse button?? like drag&drop but i dnot want to...
1
1792
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 enter the picturebox area, to draw on that direction the same picturebox with a height or width offset, and that way, no metter how farr I drag the image, the ideea of wraped on a sphere image is sesizable. I have the main routine that drag the...
10
23517
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
8547
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 g As Graphics = Graphics.FromImage(bit) Dim myPen As Pen = New Pen(Color.Blue, 1) Me.CreateGraphics.DrawLine(myPen, 0, 5, Me.Width, 5) Appreciate some help. Ed
3
2362
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 both operations at once after they have been completed? I have tried overriding OnPaint, OnBackgroundErase, etc. Double Buffering, AllPaintingInWmPaint, etc. has been set to true. No success so far. Now I'm trying to catch Windows Messages like...
8
3572
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 picture box, lots of text boxes (for input).. you get the idea. All of a sudden everything on the form has disappeared, it looks like a blank, newly created form. I can still get to the properties of every item on the form using the drop-down box in...
2
1459
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 to each other. However if they overlap they do obsure the picturebox underneath, even though the form background is still visible.
2
2775
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 Class file x.cs want to access some of the picturebox parameters e.g size but I do not have access to these parameters. I could write a function in form.cs to return these parameters but as I don't have access to an instance of form I cannot call...
2
1368
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 move but i now want the code for if the picturebox hits the side then goes another direction. The task is to make a breakout game. I have looked over the internet and all i can find is code from math nerds using x, y, z, a, b, c etc with numbers...
0
8253
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8189
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8354
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8497
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7182
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2621
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1499
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.