1

I want a label to move from the left of the form and stop at the center

I have been able to do this using

    Timer tmr = new Timer();
    int locx = 6;
    public Form1()
    {
        InitializeComponent();
        tmr.Interval = 2;
        tmr.Tick += new EventHandler(tmr_Tick);
    }

    void tmr_Tick(object sender, EventArgs e)
    {
        label1.Location = new Point(locx, 33);
        locx++;
        if (locx == 215)
        {
            tmr.Stop();
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        label1.Text = "QUICK SPARK";
        tmr.Start();
    }

I want to know if there is any better way to do this???...Any help will be appreciated

Machavity
  • 30,841
  • 27
  • 92
  • 100
  • Hans Passant has an excellent answer on the built in functionality of windows for this [here](http://stackoverflow.com/a/6103677/1324033) – Sayse Jul 22 '13 at 18:37

2 Answers2

4

If you are using VS 2012 and C# 5, you can do this simply via await/async:

private async void Form1_Load(object sender, EventArgs e)
{
    label1.Text = "QUICK SPARK";

    for (int locx = 6; locx < 215; ++locx)
    {
        await Task.Delay(2);
        label1.Location = new Point(locx, 33);
    }
}
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • Thanks but i'm actually using Visual-Studio-2005 & 2008 –  Jul 22 '13 at 18:14
  • 1
    @Precious1tj Then your option is likely the best. – Reed Copsey Jul 22 '13 at 18:14
  • I think Reed nailed it, with one more suggestion - is your form a static size, or is it user-resizeable? You might want to calculate the midpoint rather than hardcoding it. – Rob H Jul 22 '13 at 19:39
  • @RobH I agree, and think basing off the form's size is probably appropriate. However, this is going to be quite fast (less than a half second) and is happening on form load, so it's unlikely that the user will be able to resize in time for it to be a problem in practice. – Reed Copsey Jul 22 '13 at 19:41
  • True, though the other (probably more important) reason I neglected to mention was maintainability - so when the next developer comes along and is told to resize the form, he doesn't need to know that there are implicit dependencies like this. – Rob H Jul 23 '13 at 13:23
0

WinForm Animation Library [.Net3.5+]

A simple library for animating controls/values in .Net WinForm (.Net 3.5 and later). Key frame (Path) based and fully customizable.

https://falahati.github.io/WinFormAnimation/

new Animator2D(
        new Path2D(new Float2D(-100, -100), c_control.Location.ToFloat2D(), 500))
    .Play(c_control, Animator2D.KnownProperties.Location);

This moves the c_control control from -100, -100 to the location it was in first place in 500 ms.

Soroush Falahati
  • 2,196
  • 1
  • 27
  • 38