Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

How to move the object to multiple location in C#

$
0
0
Hi I am writing a visual interface map(image) on C# winform, I have the map and icon and a button start. I am looking forward to right click on the map to choose multiple location and then click the start button, the icon will go through all the point that I already click on the map. I got the problem that the icon only go to the first point and stuck in there. Here is my code so far:
 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }
        bool isClick = false;
        bool move = false;
        private Point startlocation;
        Point location1 = new Point(492, 382);
        Point location2 = new Point(543, 412);
        Point location3 = new Point(588, 429);
        Point location4 = new Point(101, 165);
        Point location5 = new Point(425, 315);
        Point location6 = new Point(552, 337);
        Point location7 = new Point(661, 329);
        Point location8 = new Point(593, 203);
        Point location9 = new Point(336, 20);
        Point location10 = new Point(500, 19);
        Point location11 = new Point(624, 90);
        Point location12 = new Point(81, 372);
        Point location13 = new Point(275, 394);
        Point location14 = new Point(393, 369);
        Point location15 = new Point(375, 549);
        List<Point> points = new List<Point>();

        // drag the icon
        private void icon_down(object sender, MouseEventArgs e)
        {
            move = true;
            if (e.Button == MouseButtons.Left)
            {
                startlocation = e.Location;
            }


        }

        private void icon_up(object sender, MouseEventArgs e)
        {
            move = false;

        }
        // drop the icon to any location
        private void icon_move(object sender, MouseEventArgs e)
        {
            if (move)
            {
                icon1.Left += e.Location.X - startlocation.X;
                icon1.Top += e.Location.Y - startlocation.Y;
            }
            // textBox1.Text = icon1.Left.ToString() + " " + icon1.Top.ToString();
        }

       // the start button to start the timer and move the icon
        private void button1_Click(object sender, EventArgs e)
        {
            isClick = true;
           
        
            timer1.Interval = 100;
            timer1.Tick += new EventHandler(timer1_Tick);
            timer1.Start();
         }
        // timer
        private void timer1_Tick(object sender, EventArgs e)
        {
            // go through list and move the icon
            if (isClick)
            {
                foreach (var pt in points)
                {
                    if (icon1.Left < pt.X)
                    {
                        icon1.Left = this.icon1.Left + 1;
                    }
                    else if (icon1.Left > pt.X)
                    {
                        icon1.Left = this.icon1.Left - 1;
                    }

                    if (icon1.Top < pt.Y)
                    {
                        icon1.Top = this.icon1.Top + 1;
                    }
                    else if (icon1.Top > pt.Y)
                    {
                        icon1.Top = this.icon1.Top - 1;
                    }
                    if ((icon1.Left == pt.X) && (icon1.Top == pt.Y))
                    {
                        timer1.Stop();
                    }
                }
            }



            
        }


       // right click to add location to points list

        private void map_mouseclick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                Point pt = e.Location;
                points.Add(pt);
             
               

            }
        }
    }
}
       


     


       
    
Any help or suggestion is appreciated, thank you
 

Viewing all articles
Browse latest Browse all 31927

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>