1 Replies - 10039 Views - Last Post: 22 July 2009 - 06:22 PM Rate Topic: -----

#1 barig4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 26-June 09

Moving a button

Post icon  Posted 22 July 2009 - 04:18 PM

I've wrote a program that is supposed to move a button from one location to another smoothly:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication9
{
	
	public partial class Form1 : Form
	{
		public int[] Velocity = new int[2];
		public int count;
		public System.Drawing.Point Dest = new Point(394, 368), Temp;
		public Form1()
		{
			InitializeComponent();
			
		}
		public int CheckV(Point a, Point b, Point Temp, int time)
		{


			int vx = 0, vy = 0;
			if (time == 1)
			{
				string sx = Convert.ToString(a.X - b.X);
				if (Math.Abs(a.X - b.X) < 1)
					return 1;
				else
				{

					if (sx.Length > 2)
						vx = (a.X - b.X) / 100;
					else if (sx.Length == 2)
						vx = (a.X - b.X) / 10;
					else
						vx = 1;

				}
				return vx;

			}
			else if (time == 2)
			{
				string sy = Convert.ToString(a.Y - b.Y);
				if (Math.Abs(a.Y - b.Y) < 1)
					return 1;
				else
				{
					if (sy.Length > 2)
						vy = (a.Y - b.Y) / 100;
					else if (sy.Length == 2)
						vy = (a.Y - b.Y) / 10;
					else
						vy = 1;

				}
				return vy;
			}
			else
				return 0;

		}
		private void button1_Click(object sender, EventArgs e)
		{
			button1.Enabled = false;
			for (int i = 0; i <= 1; i++)
			{
				Velocity[i] = CheckV(Dest, button1.Location, Temp, i + 1);
			}
			timer1.Start();
		} 

		public bool Stop(bool a, bool b)
		{
			if (button1.Location == Dest)
			{
				timer1.Stop();
				return true;
			}
			else if (a == true && b == true)
			{
				timer1.Stop();
				return true;
			}
			else return false;
		} 
		public bool Stopx()
		{
			if (Math.Abs(button1.Location.X - Dest.X) < 5 || button1.Location.X == Dest.X)
				return true;
			else return false;
		} 
		public bool Stopy()
		{
			if (Math.Abs(button1.Location.Y - Dest.Y) < 5 || button1.Location.Y == Dest.Y)
				return true;
			else return false;

		}
		
		private void timer1_Tick(object sender, EventArgs e)
		{			
			int x = 0, y = 1;
			bool stop = false, stopx = false, stopy = false;
			stopx = Stopx();
			stopy = Stopy();
			stop = Stop(stopx,stopy);
			if (stop == false)
			{
				if (stopx == false)
					Temp.X = button1.Location.X + Velocity[x];
				if (stopy == false)
					Temp.Y = button1.Location.Y + Velocity[y];
				button1.Location = Temp;
			}
			

		}
	}
}



though i cant perfect it because the Location property cant receive any non-int values, because of that if the distance between the target and the button's location is not equal for both X and Y the movement wont be smooth
the whole way.

Actually my main goal was to move a picture from one place to another if that helps anyone to help me I'll be glad to hear it.

Anyway I need help finding ideas for alternative ways to achieve my goal, help anyone?

This post has been edited by barig4: 22 July 2009 - 04:25 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Moving a button

#2 firexware  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 57
  • Joined: 06-July 09

Re: Moving a button

Posted 22 July 2009 - 06:22 PM

you wont need decimal values for the position, a pixel is the smallest unit you can move something so it should be smooth, just move it one pixel at a time until it gets to its location, it might take a bit of thinking to make the x and y coords arrive at the same time but thats simple math to figure out

take the coordinates of the button, and the coordinates it has to get to, find the slope of that line (rise over run) which is the ratio of how many pixels it needs to move vertically per pixel moved horizontally. simplify the fraction and lets say the ratio is 3:5 (3 pixels vertically, 5 horizontally), set the x velocity to 5, and the y velocity to 3 and both x and y coordinates will land on the destination at the same time
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1