dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: C# How do I Move a PictureBox slowly to the MouseClick x y position

  1. #1
    Join Date
    Jul 2011
    Posts
    4

    C# How do I Move a PictureBox slowly to the MouseClick x y position

    Hello, can someone help me with making a code to moving a picture box slow but not to slow until it meets the mouseclick x y position. The code i have right now is for the mouse click:

    protected override void OnMouseClick(MouseEventArgs e)
    {
    base.OnMouseClick(e);
    picX = e.X;
    picY = e.Y;

    }

    where picX is pictureBox1.Location.X and picY is its y location.

    I dont know the code to make the pictureBox1 location move slowly.
    Does anyone know how to do this?

  2. #2
    Join Date
    Jun 2011
    Location
    .NET4.0 / VS 2010
    Posts
    70

    Re: C# How do I Move a PictureBox slowly to the MouseClick x y position

    Well wont get any better if people give you the code so at least try it out and post what you have tried so others can help you out.

    The commands you are looking fore are pictureBox.Left = e.X; and pictureBox.Top = e.Y; One possible method to do what you want is to make ints for the new locations and have a while loop check if the positions match if not then slowly be adding to the amounts until they equal the new coordinates.

    Code:
    protected override void OnMouseClick(MouseEventArgs e)
    {
    	base.OnMouseClick(e);
    
    	int destX = e.X; // Destination of the x coordinate
    	int destY = e.Y; // Destination of the y coordinate
    
    	bool HasArrived = false; // Flag to check that if it has arrived to the destination
    
    	while (HasArrived == false)
    	{
    		// Execute code to add or subtract from values to get closer to destX and destY
    
    
    		// Check if the picture box has arrived if so then change flag to true to end loop
    		if (pictureBox.Left == destX && pictureBox.Top == destY)
    		{
    			HasArrived = true;
    		}
    	}
    }
    That is almost a fully working example of what you want to accomplish. Not sure how much speed control it would have but I'm sure you can figure something out. If not you can always post what you tried and then others can assist you with help as experience is the best teacher.

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,224

    Re: C# How do I Move a PictureBox slowly to the MouseClick x y position

    Another option is to compute a list of points that lead to the destination and use a timer to walk the list.

    In the mouse click, create the list and start the timer with a fast resolution (try 50ms to start).

    In the timer handler, walk the list (keep a _currentIndex as a field of the class), move the picture box to the coords of the current index, and increment _currentIndex. When the _currentIndex equals the list count, stop the timer and reset _currentIndex to 0.

  4. #4
    Join Date
    Jul 2011
    Posts
    4

    Re: C# How do I Move a PictureBox slowly to the MouseClick x y position

    I figured out another way to do this, thanks for the help everyone.
    I have another question, can you guys help me in my other post?
    Last edited by chris1900; July 17th, 2011 at 10:07 AM.

  5. #5
    Join Date
    Jun 2008
    Posts
    2,477

    Re: C# How do I Move a PictureBox slowly to the MouseClick x y position

    @Ubiquitous: That won't work. You are updating the position multiple times before the message pump gets to execute once. The end result will appear to be a single move to the last coordinate.

    You need to work within the windows message model, i.e., update properties, update UI (paint screen), repeat. Use a timer, set the interval, and update the position in the Tick handler.

  6. #6
    Join Date
    Jun 2011
    Location
    .NET4.0 / VS 2010
    Posts
    70

    Re: C# How do I Move a PictureBox slowly to the MouseClick x y position

    Quote Originally Posted by BigEd781 View Post
    @Ubiquitous: That won't work. You are updating the position multiple times before the message pump gets to execute once. The end result will appear to be a single move to the last coordinate.

    You need to work within the windows message model, i.e., update properties, update UI (paint screen), repeat. Use a timer, set the interval, and update the position in the Tick handler.
    Not sure what you mean I am still learning to be honest. Here is what I thought would work but doesn't really have speed control with just this.

    Code:
            protected override void OnMouseClick(MouseEventArgs e)
            {
                base.OnMouseClick(e);
    
                int newX = e.X;
                int newY = e.Y;
    
                bool arrived = false;
    
                while (arrived == false)
                {
                    //Check X coord
                    if (newX > picBox.Left)
                    {
                        picBox.Left += 1;
                    }
                    else if (newX < picBox.Left)
                    {
                        picBox.Left -= 1;
                    }
    
                    //Check Y coord
                    if (newY > picBox.Top)
                    {
                        picBox.Top += 1;
                    }
                    else if (newY < picBox.Top)
                    {
                        picBox.Top -= 1;
                    }
    
                    if (picBox.Top == newY && picBox.Left == newX)
                    {
                        arrived = true;
                    }
                }
            }
    Sorry about posting in here after the OP has already solved this issue I just wanted an insight on the code I posted.

  7. #7
    Join Date
    Jul 2011
    Posts
    3

    Re: C# How do I Move a PictureBox slowly to the MouseClick x y position

    <a href=http://www.webicon.co.in>web design</a>

  8. #8
    Join Date
    Jul 2011
    Posts
    3

    Re: C# How do I Move a PictureBox slowly to the MouseClick x y position

    Hello... good programming.........
    ==========
    web design

  9. #9
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,331

    Re: C# How do I Move a PictureBox slowly to the MouseClick x y position

    Sorry about posting in here after the OP has already solved this issue I just wanted an insight on the code I posted.
    The problem is that you are not allowing the system a chance to refresh the display which will stop the picture from being displayed at each step. It would likely just appear to jump to the final location in a single step as was pointed out before.

    A timer is a good choice as it allows the system to refresh and allows control over the speed. If you are going to do something like this in a loop at the very least you need to have a refresh statement in the loop so as to repaint the screen.
    Always use [code][/code] tags when posting code.

  10. #10
    Join Date
    Jun 2008
    Posts
    2,477

    Re: C# How do I Move a PictureBox slowly to the MouseClick x y position


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)