当前位置:编程学习 > C#/ASP.NET >>

进度条

就好像做练习题一样,显示出做题的进度,其中包含正确率和错误率之类的,以长条显示 进度条 C# --------------------编程问答-------------------- 放两个ProgressBar控件。pgbar1代表进度,pgbar2代表正确率

每次答题后更新:
pgbar1.Value = 当前做过的题目数量 * 100 / 总题数;
pgbar2.Value = 做正确的题目数量 * 100 / 当前做过的题目数量;  --------------------编程问答-------------------- 程序代码using System;
using System.ComponentModel;
using System.Windows.Forms;
namespace WindowsApplication1
{
     /// <summary>
     /// Form1 类
     /// </summary>
     public partial class Form1 : Form
     {
         public Form1()
         {
             InitializeComponent();
         }
         private void button1_Click(object sender, EventArgs e)
         {
             //用子线程工作
             new System.Threading.Thread(new System.Threading.ThreadStart(StartDownload)).Start();
         }
         //开始下载
         public void StartDownload()
         {
             Downloader downloader = new Downloader();
             downloader.onDownLoadProgress += new Downloader.dDownloadProgress(downloader_onDownLoadProgress);
             downloader.Start();
         }
         //同步更新UI
         void downloader_onDownLoadProgress(long total, long current)
         {
             if (this.InvokeRequired)
             {
                 this.Invoke(new Downloader.dDownloadProgress(downloader_onDownLoadProgress), new object[] { total, current });
             }
             else
             {
                 this.progressBar1.Maximum = (int)total;
                 this.progressBar1.Value = (int)current;
             }
         }
     }

     /// <summary>
     /// 下载类(您的复杂处理类)
     /// </summary>
     public class Downloader
     {
         //委托
         public delegate void dDownloadProgress(long total,long current);
         //事件
         public event dDownloadProgress onDownLoadProgress;
         //开始模拟工作
         public void Start()
         {
             for (int i = 0; i < 100; i++)
             {
                 if (onDownLoadProgress != null)
                     onDownLoadProgress(100, i);
                 System.Threading.Thread.Sleep(100);
             }
         }
     }
}


上面的文章是在一个博客上看到的,觉得还是可行的,还没试过,另外也在CSDN的论坛上看到过一个回复,内容如下:

关于查询统计过程显示等待提示框的问题!!!

1 楼hbxtlhx(平民百姓-自已动手,丰衣足食) 回复于 2006-12-30 11:45:46 得分 70

像这种问题要用异步来处理,在异步处理过程中Timer是可以执行的,当异步执行完成了把Timer关闭等等

因为你的程序是同步的在查询统计没有完成前程序的其它线程都处理于等待这个查询统计完成后才执行,
因此你的Timer也是在等查询统计完成后才执行就不能达到你要的效果了.Top

2 楼hbxtlhx(平民百姓-自已动手,丰衣足食) 回复于 2006-12-30 11:51:01 得分 0 
例如如下的代码就是用异步来执行的: 
delegate object dlExecuteQuery(); 
private void button1_Click(object sender, EventArgs e) 

dlExecuteQuery de=new dlExecuteQuery(this.Query()); 
IAsyncResult ir = de.BeginInvoke(null, null);

Form f=new Form() 
f.ShowDialog(this); 
Application.DoEvents(); 
while (!ir.IsCompleted) 

Application.DoEvents();








在用c#做WinFrom开发的过程中。我们经常需要用到进度条(ProgressBar)用于显示进度信息。这时候我们可能就需要用到多线程,
如果不采用多线程控制进度条,窗口很容易假死(无法适时看到进度信息)。下面我就简单结合一个我写的例子给大家做一个介绍。

第一步:设计界面不说了...注意需要引用 using System.Threading;
第二步:定义一个代理,用于更新ProgressBar的值(Value)

        //更新进度列表 
        private delegate void SetPos(int ipos);  

第三步:进度条值更新函数(参数必须跟声明的代理参数一样)

        private void SetTextMessage(int ipos) 
         { 
            if (this.InvokeRequired) 
             { 
                 SetPos setpos = new SetPos(SetTextMessage); 
                this.Invoke(setpos, new object[] { ipos}); 
             } 
            else 
             { 
                this.label1.Text = ipos.ToString() + "/100"; 
                this.progressBar1.Value = Convert.ToInt32(ipos); 
             } 
         } 

第四步:函数实现

        private void button1_Click(object sender, EventArgs e) 
         { 
             Thread fThread = new Thread(new ThreadStart(SleepT));//开辟一个新的线程 
             fThread.Start(); 
         } 

第五步:新的线程执行函数:

        private void SleepT() 
         { 
            for (int i = 0; i < 500; i++) 
             { 
                 System.Threading.Thread.Sleep(100);//没什么意思,单纯的执行延时 
                 SetTextMessage(100 * i / 500); 
             } 
         } 
 



=============================

使用浮动进度条

1.浮动窗口

   /// <summary>
        /// Increase process bar
        /// </summary>
        /// <param name="nValue">the value increased</param>
        /// <returns></returns>
        public bool Increase(int nValue)
        {

            if (nValue > 0)
            {
                if (prcBar.Value + nValue < prcBar.Maximum)
                {
                    prcBar.Value += nValue;
                    return true;
                }

                else
                {
                    prcBar.Value = prcBar.Maximum;
                    this.Close();
                    return false;
                }

            }
            return false;
        }


2.使用进度的窗口

using System.Threading;

     private frmProcessBar myProcessBar = null;
        private delegate bool IncreaseHandle(int nValue);
        private IncreaseHandle myIncrease = null;


     private void ShowProcessBar()
        {
            myProcessBar = new frmProcessBar();
            // Init increase event
            myIncrease = new IncreaseHandle(myProcessBar.Increase);
            myProcessBar.StartPosition = FormStartPosition.CenterScreen;
            myProcessBar.ShowDialog();
            myProcessBar = null;

        }
        private void ThreadFun()
        {

            MethodInvoker mi = new MethodInvoker(ShowProcessBar);
            this.BeginInvoke(mi);
            Thread.Sleep(1000);//Sleep a while to show window
            bool blnIncreased = false;
            object objReturn = null;
            do
            {
                Thread.Sleep(50);
                objReturn = this.Invoke(this.myIncrease, new object[] { 2 });
                blnIncreased = (bool)objReturn;
            }
            while (blnIncreased);
        }


使用方法:
Thread thdSub = new Thread(new ThreadStart(ThreadFun));
    thdSub.Start();
--------------------编程问答-------------------- 要源码的话加我呗 QQ昵称和csdn昵称是一样的。 --------------------编程问答-------------------- 参考这个例子:http://www.cnblogs.com/artech/archive/2008/07/30/1256144.html
使用.NET中的BackGroundWork组件 --------------------编程问答-------------------- 不是winfrom,是网站
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,