我是C#编程的新手,我很难理解如何在项目文件中放置进度条.你能帮我吗? 这是我的代码:
尝试 { lblUpdate.Visible = true ; lblUpdate.Refresh(); 字符串 []文件名= Directory.GetFiles(sTargetFolderPath); // 压缩文件-从SharpZipLib演示代码中 使用(ZipOutputStream s = 新 ZipOutputStream(File.Create(lblSaveTo.Text + " \\" + sZipFileName + " .pld" ))) { s.SetLevel( 9 ); // 0-9,其中9是最高压缩级别 字节 []缓冲区= 新 字节 [ 4096 ]; foreach (字符串文件 文件名中的字符串) { ZipEntry条目= 新 ZipEntry(Path.GetFileName(file)); entry.DateTime = DateTime.Now; s.PutNextEntry(entry); 使用(FileStream fs = File.OpenRead(file)) { int sourceBytes; 做 { sourceBytes = fs.Read(buffer, 0 ,buffer.Length); s.Write(buffer, 0 ,sourceBytes); } 同时(sourceBytes > 0 ) ; } } s.Finish(); s.Close(); } // 删除进度条 lblUpdate.Visible = false ; // 通过删除temp文件夹及其内容来清理文件 System.IO.Directory.Delete(lblSaveTo.Text + " \\ TempZipFile \\" ,是真); // 通知用户 MessageBox.Show(" 压缩文件" + lblSaveTo.Text + " 已创建." ); // 清空所有内容 lstFilePaths.Items.Clear(); lblSaveTo.Text = 字符串 .Empty; txtAddFile.Text = 字符串. } 捕获(例外) { MessageBox.Show(ex.Message.ToString()," 压缩操作错误" ); }解决方案
ProgressBar使用起来非常简单.您需要设置ProgressBar的Minimum,Maximum和Value属性. 然后在循环中使用进度条的PerformStep方法. 根据您的 使用filename.length 获取数组字符串"filenames"的长度. 字符串 []文件名= Directory.GetFiles(sTargetFolderPath);
并将其设置为进度条的最大值 将最小值设置为1 在循环中,"foreach(文件名中的字符串文件)"块结束,在该行之前使用
progressbar1.PerformStep();这是一个链接,可以为您提供一个清晰的主意, msdn.microsoft/en-us/library/system.windows.forms.progressbar.maximum.aspx [ ^ ] 希望这会有所帮助 欢呼
像这样 progressbar1.value = 0 progressbar1.maxvalue = 100 对于i作为整数= 0到100 progressbar1.value =我 下一个 //////在循环运行时,我将为进度栏提供值
I am new in c# programming and I have a hard time to understand on how to put a progress bar on my project file. Could you help me? Here is my code:
try { lblUpdate.Visible = true; lblUpdate.Refresh(); string[] filenames = Directory.GetFiles(sTargetFolderPath); // Zip up the files - From SharpZipLib Demo Code using (ZipOutputStream s = new ZipOutputStream(File.Create(lblSaveTo.Text + "\\" + sZipFileName + ".pld"))) { s.SetLevel(9); // 0-9, 9 being the highest level of compression byte[] buffer = new byte[4096]; foreach (string file in filenames) { ZipEntry entry = new ZipEntry(Path.GetFileName(file)); entry.DateTime = DateTime.Now; s.PutNextEntry(entry); using (FileStream fs = File.OpenRead(file)) { int sourceBytes; do { sourceBytes = fs.Read(buffer, 0, buffer.Length); s.Write(buffer, 0, sourceBytes); } while (sourceBytes > 0); } } s.Finish(); s.Close(); } // remove the progress bar lblUpdate.Visible = false; // clean up files by deleting the temp folder and its content System.IO.Directory.Delete(lblSaveTo.Text + "\\TempZipFile\\", true); // Notify user MessageBox.Show("Compress file " + lblSaveTo.Text + " created."); // empty everything lstFilePaths.Items.Clear(); lblSaveTo.Text = string.Empty; txtAddFile.Text = string.Empty; } catch (Exception ex) { MessageBox.Show(ex.Message.ToString(), "Compression Operation Error"); } 解决方案 ProgressBar is fairly simple to use. You need to set Minimum, Maximum and Value property of the ProgressBar. Then in the loop use PerformStep method of the progressbar. As per your e.g, Get the length of the array string "filenames" using filename.length string[] filenames = Directory.GetFiles(sTargetFolderPath);and set it as maximum value of the progressbar Set the Minimum value to 1 Within the loop, where the "foreach (string file in filenames)" block ends, just before the line use
progressbar1.PerformStep();Here is a link which will give you a clear idea, msdn.microsoft/en-us/library/system.windows.forms.progressbar.maximum.aspx[^] Hope this helps cheers
do it like this progressbar1.value = 0 progressbar1.maxvalue = 100 for i as integer = 0 to 100 progressbar1.value = i next ///// i will give progress bar the value as the loop runs更多推荐
如何添加进度条以指示文件已完成处理
发布评论