进度条使用 主要掌握下边几个命令,
//水平进度条 progressBarControlH.Properties.Minimum = 0;
//1:设置最大数量,比如读取txt文件内容,先要查出行数,然后给maxinum赋值,此处是进度条的最大容量. progressBarControlH.Properties.Maximum = 100;
//2: 进度条的进度,读取一行,内部自动计算的
while (sr.Peek() > -1) { line = sr.ReadLine().ToString().Replace(" ", ""); //跳过行空格 if (line.Trim() != "") { // progressBarControlH.Text = "开始过滤字段生成文件....."; // System.Threading.Thread.Sleep(120); progressBarControlH.PerformStep(); // progressBarControlH.Position += 1; // progressBarControlH.EditValue = i + 1; //处理当前消息队列中的所有windows消息,不然进度条会不同步 System.Windows.Forms.Application.DoEvents();
......................}
下边是所有的步骤,
1:初始化进度条 ,最大值等查完txt行数赋值.
private void panelControl1_Paint(object sender, PaintEventArgs e) { //水平进度条 progressBarControlH.Properties.Minimum = 0; //progressBarControlH.Properties.Maximum = 2000000; progressBarControlH.Properties.Step = 1; progressBarControlH.Properties.ProgressViewStyle = DevExpress.XtraEditors.Controls.ProgressViewStyle.Solid; progressBarControlH.Position = 0; progressBarControlH.Properties.ShowTitle = true; progressBarControlH.Properties.PercentView = true; progressBarControlH.Properties.ProgressKind = DevExpress.XtraEditors.Controls.ProgressKind.Horizontal; }
2:读取text行数
int CalculateCount(string filepath) { //定义一个变量 int lineCount = 0; //文件路径 // string path = @"D:SQL.txt"; try { using (StreamReader sr = new StreamReader(filepath)) { //循环读取 while (sr.Peek() >= 0) { sr.ReadLine(); lineCount++; } } return lineCount; // this.label1.Text = "文件行数为:" + lineCount.ToString(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); return 0; } }
3:赋值进度条最大值,并开始统计进度
int count= CalculateCount(file); lblcount.Visible = true; lblcount.Text = count.ToString(); progressBarControlH.Properties.Maximum = count; //删除后缀 存入files类Filename属性 ,后用于存在word第一行 StreamReader sr = new StreamReader(file, System.Text.Encoding.Default); // filecontents = new Queue<FileContent>(); String line; // while (sr.EndOfStream!=true) StringBuilder sb = new StringBuilder(); while (sr.Peek() > -1) { line = sr.ReadLine().ToString().Replace(" ", ""); //跳过行空格 if (line.Trim() != "") { // progressBarControlH.Text = "开始过滤字段生成文件....."; // System.Threading.Thread.Sleep(120); progressBarControlH.PerformStep(); // progressBarControlH.Position += 1; // progressBarControlH.EditValue = i + 1; //处理当前消息队列中的所有windows消息,不然进度条会不同步 System.Windows.Forms.Application.DoEvents();
帮助文件
progressBarControl和marqueeProgressBarControl
一.progressBarControl
progressBarControl是一个进度条控件
几个重要参数
Minimum------------------进度最小数值
Maximum-----------------进度最大数值
Step----------------------每走一步的数值
Position ------------------当前值
EditValue-----------------编辑值
ProgressViewStyle--------样式(其实我没看出什么区别)
ShowTitle-----------------是否显示标题(没有设置显示百分比的时候一般显示value值)
PercentView---------------是否显示百分比(需要ShowTitle为true)
ProgressKind-------------进度条方向(横向/纵向),这只是进度的显示方向,具体控件还要自己把它拉成横向或者纵向
TextOrientation-----------进度条文本显示方向(横向/纵向)
方法
PerformStep() -----------进度条前进一步
添加两个进度条,一个横向,一个纵向,并初始化一下参数
//水平进度条 progressBarControlH.Properties.Minimum = 0; progressBarControlH.Properties.Maximum = 100; progressBarControlH.Properties.Step = 1; progressBarControlH.Properties.ProgressViewStyle = DevExpress.XtraEditors.Controls.ProgressViewStyle.Solid; progressBarControlH.Position = 0; progressBarControlH.Properties.ShowTitle = true; progressBarControlH.Properties.PercentView = true; progressBarControlH.Properties.ProgressKind = DevExpress.XtraEditors.Controls.ProgressKind.Horizontal; //垂直进度条 progressBarControlV.Properties.Minimum = 0; progressBarControlV.Properties.Maximum = 100; progressBarControlV.Properties.Step = 1; progressBarControlV.Properties.ProgressViewStyle = DevExpress.XtraEditors.Controls.ProgressViewStyle.Solid; progressBarControlV.Position = 0; progressBarControlV.Properties.ShowTitle = true; progressBarControlV.Properties.PercentView = true; progressBarControlV.Properties.ProgressKind = DevExpress.XtraEditors.Controls.ProgressKind.Vertical; progressBarControlV.Properties.TextOrientation = DevExpress.Utils.Drawing.TextOrientation.Horizontal;
当把进度条设置为纵向的时候一般需要Title或者百分比的显示还是横向的,这样看起来才舒服,所以要设置一下TextOrientation属性
调用代码:
for (int i = 0; i < 100; i++) { System.Threading.Thread.Sleep(120); progressBarControlH.PerformStep(); progressBarControlV.PerformStep(); //progressBarControl1.Position +=1; //progressBarControl1.EditValue = i + 1; //处理当前消息队列中的所有windows消息,不然进度条会不同步 System.Windows.Forms.Application.DoEvents(); } progressBarControlH.EditValue = 0; progressBarControlV.EditValue = 0;
让进度条移动有三种方式
1.改变Position的值
2.改变EditValue的值
3.用PerformStep方法
不过要注意的是,这三种方式后面都要加上System.Windows.Forms.Application.DoEvents(),就是每次改变进度的时候都要让windows处理当前队列的消息,以确保进度条同步,否则可能出现进度条的数据更改了,可是进度没有动的情况
二.marqueeProgressBarControl
marqueeProgressBarControl用起来比较简单,但是没有ProgressBarControl的显示进度和百分比的功能
marqueeProgressBarControlH.Properties.ProgressKind = DevExpress.XtraEditors.Controls.ProgressKind.Horizontal; marqueeProgressBarControlH.Properties.ShowTitle = true; marqueeProgressBarControlH.Text = "横向读取数据中,请稍等。。。。"; marqueeProgressBarControlV.Properties.ProgressKind = DevExpress.XtraEditors.Controls.ProgressKind.Vertical; marqueeProgressBarControlV.Properties.ShowTitle = true; marqueeProgressBarControlV.Text = "纵向数据读取中,请稍等"; marqueeProgressBarControlV.Properties.TextOrientation = DevExpress.Utils.Drawing.TextOrientation.VerticalUpwards;
最后效果