如何这样添加进度条? 我目前在做个文件处理程序,所读入的文件很大,因此加载时我想加个进度条,当开始读取文件时显示进度条,加载完毕时进度条消失,现在的问题就是进度条显示的进度要如何与加载文件保持同步呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 最小值0,最大值1开始读取文件时显示进度条 visible=true,加载完毕时进度条消失visible=false 根据已处理的文件个数或者处理的字节数比例定时callback进度条更新 ……可以设个最大值比如是文件的总size,然后再读取的时候把已经读取了的文件size设进去 怎样在程序中获取要处理文件的size呢?文件大小不是固定的 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(); 楼上的没看多懂,你的意思是再加一个winform窗口用来放置进度条么?要是进度条放在状态栏控件里又该如何写代码? 欢迎有志之士加入QQ群:17257673,我们将一起探讨.net开发中的问题,打造vs.net精英。 你要是把进度条放在主窗体中(比如状态栏),就建立如下一个方法: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(); //返回文件内容} 0.01到99999 9999.99的正则表达式?? 关于库存的问题。算单价的问题。。 如何修改machine.config 一个关于dataset的数据问题!!!!!!!!!!!!!!!!!!!!!!11 三青年之C#版 欢迎大家补充 IE打印框架页的问题 急~~~~~ winform 数据显示棘手问题 谁有符合一下要求的小软件? WinForm 的线程问题!!!200分!求解答..... 半小时揭贴 经验题(`Listview相关) 把结构转换为Message的LPARAM或者WPARAM(IntPtr)
开始读取文件时显示进度条 visible=true,加载完毕时进度条消失visible=false
===============================================
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();
{
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(); //返回文件内容
}