I am using Visual Studio 2010, and I am trying to make a label that moves randomly in the form window,

Like this:

Click Here

I just want a hint on how can I make the label move randomly?

Thanks in advance.

Try :

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

namespace WindowsFormsApplication1
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        Control actcontrol;
        Point preloc;

        private void label1_MouseDown(object sender, MouseEventArgs e)
        {
            actcontrol = sender as Control;
            preloc = e.Location;
            Cursor = Cursors.Default;
        }

        private void label1_MouseMove(object sender, MouseEventArgs e)
        {
            if (actcontrol == null || actcontrol != sender)
                return;
            var location = actcontrol.Location;
            location.Offset(e.Location.X - preloc.X, e.Location.Y - preloc.Y);
            actcontrol.Location = location;
        }

        private void label1_MouseUp(object sender, MouseEventArgs e)
        {
            actcontrol = null;
            Cursor = Cursors.Default;
        }
    }
}

Just in case you wanted automatic movement here's a simple way. You need a label, a button and a timer

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

namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {        
        Point Max = new Point();
        Point NewPoint = new Point();
        //Initialize random number generator
        Random Rnd = new Random();
        public Form1()
        {
            InitializeComponent();
            //Set Max point to form size minus the label size so that the whole label will always fit on the form.
            Max = new Point(this.ClientSize - label1.Size);
            timer1.Interval = 500;
            //Reset Max whenever the form is resized.
            this.Resize +=new EventHandler(Form1_Resize);
        }
        private void Form1_Resize(object sender, EventArgs e)
        {
            Max = new Point(this.ClientSize - label1.Size);
        }
        //Return a random point to move the label to
        private Point RandomPoint()
        {
            NewPoint.X = Rnd.Next(Max.X);
            NewPoint.Y = Rnd.Next(Max.Y);
            return NewPoint;
        }
        //Move the label after each interval of the timer
        private void timer1_Tick(object sender, EventArgs e)
        {
            label1.Location = RandomPoint();            
        }
        //Toggle the timer on and off
        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = !timer1.Enabled;
        }
    }
}

Edited 3 Years Ago by tinstaafl

@tinstaafl,

Thanks, but it didn't really work as I want. Maybe I said it in a wrong way in the first post :S.
I don't want the label to be at for example: (40,10) and then in the next second it moves for ex: to(5,24) I want it to move like in the image I posted in first post. Not jumping from one location to another.

Thanks anyway.

I want it to move like in the image I posted in first post. Not jumping from one location to another.

then you should try my suggested code

Edited 3 Years Ago by Jx_Man

@Jx Man,
I also tried your code, but the label is not even moving. I see also that all the methods in your code are about the mouse and I am not caring about the mouse, I just want the label to move.

Thanks :)

Well.. it works fine to me and its not about the mouse. You need to understand the codes more.
i'm traping the mouse event on label. It will make the label to follow the locations of mouse.
Just press the label and drag it around the form.

Edited 3 Years Ago by Jx_Man

@Jx Man, thanks but I want the label to move automatically and exactly
as the blue squares in this link:
Click Here

I don't want to move it manually with my mouse.

Here's a demonstration of a simple algorithm for moving the label around the screen. This uses a random function to find the end point to move to, but that can be calculated however you like. The code restricts the movement so that it stays in the form. To slow down the speed uncomment the thread.sleep statement

    public partial class Form1 : Form
    {
        Point Max = new Point();
        Point EndPoint = new Point();
        Point StartPoint = new Point();
        //Initialize random number generator
        Random Rnd = new Random();
        public Form1()
        {
            InitializeComponent();
            //Set Max point to form size minus the label size so that the whole label will always fit on the form.
            Max = new Point(this.ClientSize - label1.Size);
        }
        //Return a random point to move the label to
        private Point RandomPoint()
        {
            Point NewPoint = new Point();
            NewPoint.X = Rnd.Next(1, Max.X);
            NewPoint.Y = Rnd.Next(1, Max.Y);
            return NewPoint;
        }       
        private void button1_Click_1(object sender, EventArgs e)
        {
            EndPoint = RandomPoint();
            StartPoint = label1.Location;
            double MaxSteps = 0;
            double StepsX = EndPoint.X - StartPoint.X;
            double StepsY = EndPoint.Y - StartPoint.Y;
            double StepX = 0;
            double StepY = 0;
            double NewX = 0;
            double NewY = 0;
            //Find which coordinate has the biggest change.  The biggest one will step by 1 and the smaller one will step
            //by a fraction of 1 so that it ends up at the right coordinates after the same number of steps.  This has to be
            //rounded to an int so in effect it will only change every certain number of steps.
            //Setting the sign of the step regulates forward and backward movement
            //MaxSteps is to give the for loop it's upper bound.
            if (Math.Abs(StepsX) > Math.Abs(StepsY))
            {
                StepX = 1 * Math.Sign(StepsX);
                MaxSteps = StepsX;
                StepY = (Math.Abs(StepsY) / Math.Abs(StepsX)) * Math.Sign(StepsY);
            }
            else
            {
                StepY = 1 * Math.Sign(StepsY);
                MaxSteps = StepsY;
                StepX = (Math.Abs(StepsX) / Math.Abs(StepsY)) * Math.Sign(StepsX);
            }
            for (int i = 1; i <= Math.Abs(MaxSteps); i++)
            {
                NewX += StepX;
                NewY += StepY;
                label1.Location = new Point((int)(Math.Round(StartPoint.X + NewX)), (int)(Math.Round(StartPoint.Y + NewY)));                    
                //remove this to make the motion even faster.
                this.Refresh();
                //Thread.Sleep(100);
            }
        }
    }

Edited 3 Years Ago by tinstaafl

Ok Now its working but can't the label move by itself without me clicking on button?

Thanks.

Ok nevermind my previous post.
I just cut the code you wrote in the button1_click method to Form1_MouseMove. But the label is moving too fast.
How can I reduce its speed ?

This article has been dead for over six months. Start a new discussion instead.