1

我有:

private void button1_MouseEnter(object sender, EventArgs e)
{
    for (int i = 0; i > 2; i++) 
    {     
        button1.Content = Convert.ToString(i);
        System.Threading.Thread.Sleep(1000);     
    }

    tekst.Text = "Mouse Enter";
}

当我进入时,Button我只看到,Mouse Enter但不要更改。为什么?我能做错什么?ContentButton

4

2 回答 2

2

嗨,你的 for 循环正确吗?它应该是 i<2 而不是 i>2

 for (int i = 0; i < 2; i++)
        {
于 2013-03-07T19:04:11.953 回答
0

您的 for 循环永远不会执行,因为您有错误的条件,将其更改为以下代码:

for (int i = 0; i < 2; i++)

您还应该使用BackgroundWorker( msdn ) 动态更新您的 GUI。

private void button1_MouseEnter(object sender, MouseEventArgs e)
{
    BackgroundWorker worker = new BackgroundWorker();
    worker.DoWork += delegate
    {
        for (int i = 0; i < 2; i++)
        {
            this.Dispatcher.Invoke((Action)(() => { btn.Content = Convert.ToString(i); }));
            System.Threading.Thread.Sleep(1000);
        }                
    };
    worker.RunWorkerCompleted += delegate { tekst.Text = "Mouse Enter"; };
    worker.RunWorkerAsync();
}
于 2013-03-07T19:12:47.020 回答