Change button content in WPF

前端 未结 2 778
攒了一身酷
攒了一身酷 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:40

    Hi is your for loop correct? It should be i<2 instead of i>2

     for (int i = 0; i < 2; i++)
            {
    
    0 讨论(0)
  • 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();
    }
    
    0 讨论(0)
提交回复
热议问题