int rowcount = myData.Tables[0].Rows.Count;
            progressBar1.Visible = true;
            progressBar1.Minimum = 0;
            progressBar1.Maximum = myData.Tables[0].Rows.Count;
            for (int i = 0; i < myData.Tables[0].Rows.Count; i++)
            {
                label1.Text = "正在转化:  " + i + "/" + rowcount;
                myExcel.Cells[2 + i, 1] = myData.Tables[0].Rows[i]["japanese"];
                myExcel.Cells[2 + i, 2] = myData.Tables[0].Rows[i]["jiaming"];
                myExcel.Cells[2 + i, 3] = myData.Tables[0].Rows[i]["type"];
                myExcel.Cells[2 + i, 4] = myData.Tables[0].Rows[i]["chinese"];
                myExcel.Cells[2 + i, 5] = myData.Tables[0].Rows[i]["lesson"];
                myExcel.Cells[2 + i, 6] = myData.Tables[0].Rows[i]["unit"];
                progressBar1.Value = i;
            }
我的代码是想label显示运行到第几个文件了,但是运行的时候,只有进度条向前滚动,label的值根本不变,这是为什么?

解决方案 »

  1.   

    for中加入Application.DoEvents();
      

  2.   

    ...
    for (int i = 0; i < myData.Tables[0].Rows.Count; i++) 
    {      
         label1.Text = "正在转化:  " + i + "/" + rowcount;
         refresh() ;
         ...

    ...
    public void refresh() 

    Application.DoEvents(); 
    System.Threading.Thread.Sleep(100);
    SetStyle(ControlStyles.SupportsTransparentBackColor,true); 
    SetStyle(ControlStyles.AllPaintingInWmPaint, true); 
    SetStyle(ControlStyles.UserPaint, true); 
    SetStyle(ControlStyles.DoubleBuffer, true); 
    Refresh(); 
    }
      

  3.   

    label1.Text = "正在转化:  " + i.ToString() + "/";
    label1.Refresh();