实现winform进度条?

zkvistor 2009-01-09 03:49:26
WinForm窗体中发短信要实现一个进度条,找了一个是用循环做的,进度条的速度太快了,效果不好,怎么实现效果会好一些?用backgroundWorker,具体怎么实现,能否附上一些代码?
...全文
5350 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
xb12369 2012-04-24
  • 打赏
  • 举报
回复
额,大家可能想复杂了哦!!

lz在创体力拖一个Lable,一个button,一个progressBar1

具体code如下:去不铁给你,直接用

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace WinTestUrl
{
public delegate void SetPos(int ipos);
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}

public void SetTextMessage(int ipos)
{
if (this.InvokeRequired)
{
SetPos setpos = new SetPos(SetTextMessage);
this.Invoke(setpos, new object[] { ipos });
}
else
{
this.label1.Text = ipos.ToString() + "/10";
this.progressBar1.Value = Convert.ToInt32(ipos);
}
}

public void button2_Click(object sender, EventArgs e)
{
Thread fThread = new Thread(new ThreadStart(SleepT));//开辟一个新的线程
fThread.Start();
}

public void SleepT()
{
for (int i = 1; i <= 100; i++)
{
System.Threading.Thread.Sleep(100);//没什么意思,单纯的执行延时
SetTextMessage(10 * i / 100);
}
}


}
}

wangjile713 2011-09-29
  • 打赏
  • 举报
回复
顶 帮忙顶爱好
xiaowangziqiushui 2011-09-28
  • 打赏
  • 举报
回复
帮顶,呵呵...
快乐小子飞 2010-09-17
  • 打赏
  • 举报
回复
这个效果是从100线程减到0,进度条的显示。

public Thread th1;

delegate void SetValues(int i);
delegate void ShowProBar(int i);

private void button1_Click(object sender, EventArgs e)
{
th1 = new Thread(new ThreadStart(test1));
th1.Start();
}
private void test1()
{
int i = 100;
while (i > -1)
{
setvalue1(i);
ShowProgress1(i);
i--;
Thread.Sleep(1000);
}
}
private void setvalue1(int i)
{
if (this.InvokeRequired)
{
this.Invoke(new SetValues(setvalue1), new object[] {i});
}
else
{
自己的操作
}
}
private void ShowProgress1(int i)
{
if (this.progressBar1.InvokeRequired)
{
this.Invoke(new ShowProBar(ShowProgress1), new object[] { i });
}
else
{
progressBar1.Value = 100-i;
progressBar1.Visible=true;
}
}
coosiu 2010-04-10
  • 打赏
  • 举报
回复
我也在找这个问题难道大家爱没有很好的解答吗???
醉春风2014 2010-02-22
  • 打赏
  • 举报
回复
scy251147 2009-01-09
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 zkvistor 的回复:]
引用 3 楼 CrystalofAA 的回复:
怎么实现效果会好一些?我觉得贴个图片效果就很好了 呵呵



效果不好是因为用循环做,进度条加载的速度太快了,和图片没关系
[/Quote]
那就用多线程吧,这个比较好
zkvistor 2009-01-09
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 CrystalofAA 的回复:]
怎么实现效果会好一些?我觉得贴个图片效果就很好了 呵呵
[/Quote]


效果不好是因为用循环做,进度条加载的速度太快了,和图片没关系
优途科技 2009-01-09
  • 打赏
  • 举报
回复
多线程。
CrystalofAA 2009-01-09
  • 打赏
  • 举报
回复
怎么实现效果会好一些?我觉得贴个图片效果就很好了 呵呵
zetee 2009-01-09
  • 打赏
  • 举报
回复
http://www.chenjiliang.com/Article/Default.aspx?TypeID=98

这里。都是多线程的,看看吧
qimini 2009-01-09
  • 打赏
  • 举报
回复
后台类:
public class PublishHelper
{
private static PublishHelper helper;
public static PublishHelper Instance
{
get
{
if ( helper == null )
{
helper = new PublishHelper();
}
return helper;
}
}


/// <summary>
/// 获取文件列表
/// </summary>
/// <param name="BaseDir">APP地址</param>
/// <returns>List列表</returns>
public AssemblyInfoFileList GetFileList( string BaseDir )
{
AssemblyInfoFileList afl = GetClientUpdateList( BaseDir );
return afl;
}

/// <summary>
/// 私有方法,递归数据给上面客户端列表
/// </summary>
/// <param name="BaseDir">APP地址</param>
/// <returns>List列表</returns>
private AssemblyInfoFileList GetClientUpdateList( string BaseDir )
{
if ( String.IsNullOrEmpty( BaseDir ) )
{
BaseDir = Path.Combine( AppDomain.CurrentDomain.BaseDirectory, "UpdateClient" );
}

return AssemblyHelper.GetAssemblyList( BaseDir );
}


/// <summary>
/// 进行文件COPY的方法[线程回调]
/// </summary>
/// <param name="filelist">文件列表</param>
/// <param name="UserPath">用户选择的路径</param>
/// <param name="bar">进度条</param>
/// <param name="lab">标签条</param>
public void ShareSession( AssemblyInfoFileList filelist, string UserPath, string publishPath,ProgressBar bar, Label lab )
{
int cm = 100 / filelist.Count;

if ( File.Exists( AssemblyHelper.AssemblyListFileName ) ) File.Delete( AssemblyHelper.AssemblyListFileName );
//string AppPath =Path.Combine( System.AppDomain.CurrentDomain.BaseDirectory, "UpdateClient" );
string AppPath = Path.Combine(publishPath, "UpdateClient");
if (!Directory.Exists(AppPath))
{
Directory.CreateDirectory(AppPath);
}
foreach ( AssemblyInfoFile temp in filelist )
{
if ( bar.Value < 100 ) { bar.Value += cm; }
lab.Text = "正在下载:" + temp.RelativePath;
string remoteFileName = temp.RelativePath;
if ( remoteFileName.IndexOf( "\\" ) > 0 )
{
string file = remoteFileName.Substring( remoteFileName.LastIndexOf( "\\" ) + 1, remoteFileName.Length - remoteFileName.LastIndexOf( "\\" ) - 1 );
string[] spli = { "\\" + file };
string path = remoteFileName.Split( spli, StringSplitOptions.RemoveEmptyEntries )[ 0 ];
if ( !Directory.Exists( Path.Combine( AppPath, path ) ) )
{
Directory.CreateDirectory( Path.Combine( AppPath, path ) );
}
}
File.Copy( Path.Combine( UserPath, remoteFileName ), Path.Combine( AppPath, remoteFileName ), true );
}
bar.Value = 100;
lab.Text = "文件更新成功完成";
}

}


界面调用:

string strAppCode = this.tslAppCode.Text;

if ( this.UpdatedFile.Count == 0 || String.IsNullOrEmpty( strAppCode ) || strAppCode == "应用系统" )
{
return;
}

try
{
if ( MessageBox.Show( String.Format( "您确定要更新文件夹{0}下的{1}个文件到{2}系统么? ", this.LocalRootDirectory.FullName,
this.UpdatedFile.Count, strAppCode ), "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question )
== DialogResult.No )
return;

tsbUpdate.Enabled = false;
ProgressBarStatus.Visible = true;
ProgressBarStatus.Maximum = UpdatedFile.Count;
ProgressBarStatus.Value = 0;

helper.UploadWholeDir = false;
helper.UploadFile( this.UpdatedFile, strAppCode );

MessageBox.Show( "更新服务完成", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information );
}
catch ( Exception ex )
{
MessageBox.Show( "更新失败" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error );
LogManager.WriteError( ex );
}
finally
{
ProgressBarStatus.Visible = false;
this.labelInfo.Text = "就绪";
tsbUpdate.Enabled = true;
}

110,534

社区成员

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

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

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