进度条(ProgressBar)的用法?

优途科技 2007-06-20 04:10:31
如题:比如我文件复制的时候,显示复制进度。那么对控件ProgressBar我要怎么做?原理或者代码!谢谢!
...全文
7118 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
luffy927 2007-06-21
  • 打赏
  • 举报
回复
还有个法子就是调用A,P,I。得在网上搜了。
扁豆肉丝 2007-06-21
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace WindowsApplication1
{
public partial class Form2 : Form
{
static int sum;
int sum1 = 0;
string strin;
string strout;

public Form2()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
strin = this.textBox1.Text;
strout = this.textBox2.Text;

if (!strin.Substring(strin.Length - 1).Equals("\\"))
{
strin = strin + "\\";
}


if (!strout.Substring(strout.Length - 1).Equals("\\"))
{
strout = strout + "\\";
}



jisuan(strin);


this.label1.Text = sum.ToString();
Form3 f3 = new Form3(sum,strin,strout);

DialogResult dia = f3.ShowDialog();
if (dia == DialogResult.OK) {
MessageBox.Show("ok");
System.Diagnostics.Process.Start(strout);
}



}
private void jisuan(string strin)
{
DirectoryInfo d = new DirectoryInfo(strin);
FileSystemInfo[] f = d.GetFileSystemInfos();
foreach (FileSystemInfo i in f)
{
if (i is DirectoryInfo)
{



jisuan( strin + i.Name + "\\");
}
else if (i is FileInfo)
{

sum++;

}
}
}

public void copyDir(string strin,string strout,BackgroundWorker bw,DoWorkEventArgs e)
{


DirectoryInfo d = new DirectoryInfo(strin);
FileSystemInfo[] f = d.GetFileSystemInfos();
foreach (FileSystemInfo i in f)
{
if (i is DirectoryInfo)
{
DirectoryInfo di = new DirectoryInfo(strout+i.Name);
di.Create();


copyDir(strin + i.Name + "\\", strout + i.Name + "\\",bw,e);
}
else if (i is FileInfo)
{
((FileInfo)i).CopyTo(strout + i.Name, true);
sum1++;
int j = 100 * sum1 / sum;
bw.ReportProgress(j);
}
}



}

private void button3_Click(object sender, EventArgs e)
{
this.folderBrowserDialog1.SelectedPath = "c://";
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
this.textBox1.Text = folderBrowserDialog1.SelectedPath;

}
}

private void button4_Click(object sender, EventArgs e)
{

this.folderBrowserDialog1.SelectedPath = "d://";
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
this.textBox2.Text = folderBrowserDialog1.SelectedPath;

}
}

private void Form2_Load(object sender, EventArgs e)
{
strin = this.textBox1.Text;
strout = this.textBox2.Text;
}


}
}
扁豆肉丝 2007-06-21
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
public partial class Form3 : Form
{
int sum=0;
string strin;
string strout;

public Form3()
{
InitializeComponent();
}
public Form3(int sum,string strin,string strout)
{
InitializeComponent();
this.sum = sum;
this.strin = strin;
this.strout = strout;
}
private void Form3_Load(object sender, EventArgs e)
{
this.progressBar1.Maximum = sum;
this.progressBar1.Step = 1;
this.backgroundWorker1.RunWorkerAsync();
}

//backgroundWorker为了重复调用进度条 也是窗体间传参
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{

BackgroundWorker send = (BackgroundWorker)sender;
Form2 f22 = new Form2();
f22.copyDir(strin,strout,send,e);


}


private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.progressBar1.PerformStep();
}

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
this.Close();

this.DialogResult = DialogResult.OK;
}

private void button2_Click(object sender, EventArgs e)
{
this.backgroundWorker1.CancelAsync();
}
}
}
扁豆肉丝 2007-06-21
  • 打赏
  • 举报
回复
backgroundWorker的应用
因为两个工作是并进的,所以需要在循环中增加进度条
一种方法
this.backgroundWorker1.RunWorkerAsync();
启动到dowork

dowork
做事情()

progresschange 状态变化
做事情()
bw.ReportProgress(j);触发progresschange

progresschange方法中
this.progressBar1.PerformStep();

这种方法就是用backgroundWorker来调用PerformStep

backgroundWorker的作用就是窗体互调。和delegate相似。多的就是知

道什么时候工作结束,RunWorkerCompleted{this.Close();}还有可以中

断CancelAsync


还一种方法
直接调用prossece.PerformStep()
这样就必须在form中声明
prossece=new prossece()
而不能用另外的类
优途科技 2007-06-20
  • 打赏
  • 举报
回复
谢谢了
luffy927 2007-06-20
  • 打赏
  • 举报
回复
所以说自己写个copy函数么,呵呵。这样,每次把文件读出来一些,读多少自己就知道了, value值就知道了。
优途科技 2007-06-20
  • 打赏
  • 举报
回复
luffy927() 还在吗?
有人指点吗?谢谢大家了!
adf3wswwe3 2007-06-20
  • 打赏
  • 举报
回复
Minimum 加 1
优途科技 2007-06-20
  • 打赏
  • 举报
回复
已读取大小 他是一个变量,也就是说Copy函数此时正在执行,if(已读取大小>总大小/100)函数放在甚么地方?

File.COPy(strq,str2);
if(已读取大小>总大小/100)
{
ProcessUpdateEventArgs Args = new ProcessUpdateEventArgs(//传递参数,比如已读取/拷贝的文件大小);
ProcessUpdated(this, Args);//触发事件。
}

这样肯定不行!
优途科技 2007-06-20
  • 打赏
  • 举报
回复
luffy927() 谢谢!
还有一点,马上给分:)


if(已读取大小>总大小/100) //已读取大小在甚么地方获取,此时Copy函数已经开始执行了啊!?
{
ProcessUpdateEventArgs Args = new ProcessUpdateEventArgs(//传递参数,比如已读取/拷贝的文件大小);
ProcessUpdated(this, Args);//触发事件。
}
luffy927 2007-06-20
  • 打赏
  • 举报
回复
不用,你自己写个copy程序就方便点,读取多少了之后触发事件。
比如要触发100次就
if(已读取大小>总大小/100)
{
ProcessUpdateEventArgs Args = new ProcessUpdateEventArgs(//传递参数,比如已读取/拷贝的文件大小);
ProcessUpdated(this, Args);//触发事件。
}
优途科技 2007-06-20
  • 打赏
  • 举报
回复
是不是和甚么线程进程有关系?
优途科技 2007-06-20
  • 打赏
  • 举报
回复
关键是在拷贝的过程中,我还需要适时的获得复制文件的大小,这一点我很迷茫,Copy函数正在执行,怎么获得已经复制文件的大小?等他完成了,也就没有必要查看复制大小了,就在这一点上,有点迷糊,望指点!
luffy927 2007-06-20
  • 打赏
  • 举报
回复
先获得总大小。再开始copy。
拷贝的大小即value值可以在过程中取得。
优途科技 2007-06-20
  • 打赏
  • 举报
回复
本机
优途科技 2007-06-20
  • 打赏
  • 举报
回复
请帮忙,呵呵,最好有源代码!新手理解很困难!谢谢:)
luffy927 2007-06-20
  • 打赏
  • 举报
回复
先获得大小。再开始copy。
获得大小要看你是从哪拷贝。ftp,http,本机
优途科技 2007-06-20
  • 打赏
  • 举报
回复
还有问题,怎么获得楼上所说的所拷文件的大小,比如File.Copy正在运行,那么我怎么去获得已经拷贝过去文件的大小呢?谢谢!
wzuomin 2007-06-20
  • 打赏
  • 举报
回复
顶luffy927的方法。哈
优途科技 2007-06-20
  • 打赏
  • 举报
回复
谢谢,try it
加载更多回复(6)

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧