我目前在做个文件处理程序,所读入的文件很大,因此加载时我想加个进度条,当开始读取文件时显示进度条,加载完毕时进度条消失,现在的问题就是进度条显示的进度要如何与加载文件保持同步呢?

解决方案 »

  1.   

    最小值0,最大值1
    开始读取文件时显示进度条 visible=true,加载完毕时进度条消失visible=false
      

  2.   

    根据已处理的文件个数或者处理的字节数比例定时callback进度条更新
      

  3.   

    ……可以设个最大值比如是文件的总size,然后再读取的时候把已经读取了的文件size设进去
      

  4.   

    怎样在程序中获取要处理文件的size呢?文件大小不是固定的
      

  5.   

    in your MainForm:
    ===============================================
    processForm.ShowDialog(this);
    string fileContext = processForm.fileContext;
    processForm.Dispose();
    in your ProcessForm;
    ===============================================
    FileInfo fi = new FileInfo(filePath);
    Int64 pMax = fi.Length;Double step = Int32.MaxValue / fi.Length;in your File_Process_Method of ProcessForm:
    ==============================================================
    progressBar.Minimum = 0; progressBar.Maximum = Int32.MaxValue; StreamReader sr = new StreamReader(filePath);
    StringBuilder sb = new StringBuilder();
    String str;
    while((str = sr.ReadLine()) != null)
    {
    sb.Append(str);
    progressBar.Value = (Int32)(step * sr.BaseStream.Position);
    }
    sr.Close();
    fileContext = sb.ToString();
      

  6.   

    楼上的没看多懂,你的意思是再加一个winform窗口用来放置进度条么?要是进度条放在状态栏控件里又该如何写代码?
      

  7.   

    欢迎有志之士加入QQ群:17257673,我们将一起探讨.net开发中的问题,打造vs.net精英。
      

  8.   

    你要是把进度条放在主窗体中(比如状态栏),就建立如下一个方法:private String OpenFileEx(String filePath)
    {
    FileInfo fi = new FileInfo(filePath);Int64 pMax = fi.Length;  //得到文件长度
    Double step = Int32.MaxValue / fi.Length; //计算进度条和实际长度的比率progressBar.Minimum = 0; 
    progressBar.Maximum = Int32.MaxValue;  //设置进度条最大值,最小值StreamReader sr = new StreamReader(filePath); //打开你要处理的文件
    StringBuilder sb = new StringBuilder(); //存放文件内容(这个是为了虚拟一个文件操作)
    String str;
    while((str = sr.ReadLine()) != null) //按行读文件内容到STR这个变量
    {
       sb.Append(str); //将STR内容加入到SB中
       progressBar.Value = (Int32)(step * sr.BaseStream.Position); //设置进度条当前的进度
       Application.DoEvents(); //让进度条的进度效果能显示出来
    }
    sr.Close(); //处理结束,关闭文件流
    return sb.ToString(); //返回文件内容
    }