Change button content in WPF

前端 未结 2 780
攒了一身酷
攒了一身酷 2021-01-25 02:04

I have:

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


        
2条回答
  •  孤街浪徒
    2021-01-25 02:47

    Your for loop never execute because you have wrong condition, change it to following code:

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

    Also you should use BackgroundWorker (msdn) to update your GUI dynamicly.

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

提交回复
热议问题