简体   繁体   中英

Simple Movement in C#

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

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);
    }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM