Skip to main content
 首页 » 编程设计

c#之C#中的简单移动

2024年02月13日13kerrycode

我想要一个标签从表单的左侧移动并停在中间

我已经能够使用

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

我想知道是否有更好的方法来做到这一点???...任何帮助将不胜感激

请您参考如下方法:

如果您使用的是 VS 2012 和 C# 5,您可以通过 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); 
    } 
}