本人对C#还很懵懂刚才在CSDN搜索进度条相关资料,看了良久,头大了,但是不懂现欲求简单进度条代码和计时器代码例如:有三个控件1.开始按钮2.进度条控件3.计时器控件(有什么的好呢?郁闷啊)想实现的功能是(用C#)按下 开始按钮 
1.进度条开始运行(类似WINDOWS开机的那种方式,一遍又一遍的进度,因为我也不知道程序会运行多长时间)2.计时器开始计时 00:00:00的格式 一秒一秒地走3.运行一个循环
   循环的功能:在外部定义一个字符串
                 10秒钟执行一次(wait for 10sec),判断字符串的内容(如:var)
                 如if var=="aaa" 则跳出循环
                 否则继续10秒钟判断一次var==“aaa”                 4.如果var==“aaa”,进度和计时停止,进度条清空,但时间不清空5.下次按下开始按钮时,先将时间清空 顺序2.3.4步骤有点复杂但是感觉CSDN里的朋友们解决这个还是应该很容易的先谢谢各位朋友了

解决方案 »

  1.   


    Public Class Form1    Private WithEvents tmr As Timers.Timer
        Private WithEvents btn As Button
        Private WithEvents SetVarBtn As Button
        Private bar As ProgressBar
        Private txtbox As TextBox
        Private var As String
        Private start As Date    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            tmr = New Timers.Timer(1000)
            btn = New Button
            btn.Text = "Start"
            SetVarBtn = New Button
            SetVarBtn.Text = "Set Var=aaa"
            bar = New ProgressBar
            bar.Style = ProgressBarStyle.Blocks
            txtbox = New TextBox
            txtbox.Text = "00:00:00"
            Me.Controls.AddRange(New Control() {btn, bar, txtbox, SetVarBtn})
            btn.Location = New Point(10, 10)
            SetVarBtn.Location = New Point(10, 40)
            txtbox.Location = New Point(10, 70)
            bar.Dock = DockStyle.Bottom
        End Sub    Private Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn.Click
            var = ""
            tmr.Start()
            start = Now
            bar.Style = ProgressBarStyle.Marquee
            Dim th As New Threading.Thread(AddressOf DoLoop)
            th.IsBackground = True
            th.Start()
        End Sub    Private Sub DoLoop()
            While Not (var = "aaa")
                Threading.Thread.Sleep(10 * 1000)
            End While
            Me.Invoke(New RunPause(AddressOf Pause))
        End Sub    Private Delegate Sub RunPause()
        Private Sub Pause()
            bar.Style = ProgressBarStyle.Blocks
            tmr.Stop()
        End Sub    Private Sub tmr_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles tmr.Elapsed
            Dim str As String = String.Format("{0}:{1}:{2}", DateDiff(DateInterval.Hour, start, Now).ToString.PadLeft(2, "0"), _
                                                         DateDiff(DateInterval.Minute, start, Now).ToString.PadLeft(2, "0"), _
                                                         DateDiff(DateInterval.Second, start, Now).ToString.PadLeft(2, "0"))
            Me.Invoke(New RunModifyText(AddressOf ModifyText), str)
        End Sub    Private Delegate Sub RunModifyText(ByVal str As String)
        Private Sub ModifyText(ByVal str As String)
            txtbox.Text = str
        End Sub    Private Sub SetVarBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SetVarBtn.Click
            var = "aaa"
        End Sub
    End Class
      

  2.   

    这是软件自动由vb转成c#的public class Form1 

        
        private Timers.Timer tmr; 
        private Button btn; 
        private Button SetVarBtn; 
        private ProgressBar bar; 
        private TextBox txtbox; 
        private string var; 
        private System.DateTime start; 
        
        private void Form1_Load(object sender, System.EventArgs e) 
        { 
            tmr = new Timers.Timer(1000); 
            btn = new Button(); 
            btn.Text = "Start"; 
            SetVarBtn = new Button(); 
            SetVarBtn.Text = "Set Var=aaa"; 
            bar = new ProgressBar(); 
            bar.Style = ProgressBarStyle.Blocks; 
            txtbox = new TextBox(); 
            txtbox.Text = "00:00:00"; 
            this.Controls.AddRange(new Control[] {btn, bar, txtbox, SetVarBtn}); 
            btn.Location = new Point(10, 10); 
            SetVarBtn.Location = new Point(10, 40); 
            txtbox.Location = new Point(10, 70); 
            bar.Dock = DockStyle.Bottom; 
        } 
        
        private void btn_Click(object sender, System.EventArgs e) 
        { 
            var = ""; 
            tmr.Start(); 
            start = Now; 
            bar.Style = ProgressBarStyle.Marquee; 
            Threading.Thread th = new Threading.Thread(DoLoop); 
            th.IsBackground = true; 
            th.Start(); 
        } 
        
        private void DoLoop() 
        { 
            while (!(var == "aaa")) { 
                Threading.Thread.Sleep(10 * 1000); 
            } 
            this.Invoke(new RunPause(Pause)); 
        } 
        
        private delegate void RunPause(); 
        private void Pause() 
        { 
            bar.Style = ProgressBarStyle.Blocks; 
            tmr.Stop(); 
        } 
        
        private void tmr_Elapsed(object sender, System.Timers.ElapsedEventArgs e) 
        { 
            string str = string.Format("{0}:{1}:{2}", DateDiff(DateInterval.Hour, start, Now).ToString.PadLeft(2, "0"), DateDiff(DateInterval.Minute, start, Now).ToString.PadLeft(2, "0"), DateDiff(DateInterval.Second, start, Now).ToString.PadLeft(2, "0")); 
            this.Invoke(new RunModifyText(ModifyText), str); 
        } 
        
        private delegate void RunModifyText(string str); 
        private void ModifyText(string str) 
        { 
            txtbox.Text = str; 
        } 
        
        private void SetVarBtn_Click(object sender, System.EventArgs e) 
        { 
            var = "aaa"; 
        } 

      

  3.   

    用ProgressBar控件
     bar.Style = ProgressBarStyle.Marquee是何证左右动的楼上写的很清哦,自己看看!
      

  4.   

    1.进度条开始运行(类似WINDOWS开机的那种方式,一遍又一遍的进度,因为我也不知道程序会运行多长时间)
    ---------
     使用循环(滑动)图片模拟...2.计时器开始计时 00:00:00的格式 一秒一秒地走
    ---------
     使用Timer控件, 属性Interval设为1000, 再用Label显示...
      

  5.   

    我给楼主弄个简单的:
    1.在界面上拖两个按钮(button1,button2),一个滚动条(progressBar1),一个计时器(timer1),一个文本框(label1)2.设置计时器的Interval属性为1000,让它每秒动执行一次;label1的text为0,计数从0开始.3.通过异步调用实现滚动条:        private void timer1_Tick(object sender, EventArgs e)
            {
                label1.Text = (Convert.ToInt32(label1.Text) + 1).ToString();//每秒动label1的值加1
            }//按钮1开始
            private void button1_Click(object sender, EventArgs e)
            {
                progressBar1.Style = ProgressBarStyle.Marquee;
                timer1.Start();            MethodInvoker mi = new MethodInvoker(DoSomething1);
                mi.BeginInvoke(null, null);
            }//按钮2停止
    private void button2_Click(object sender, EventArgs e)
            {
                var="aaa";
            }
            //完成
            private void Finish()
            {
                progressBar1.Style = ProgressBarStyle.Blocks;
                timer1.Stop();
            }        //循环方法
            private void DoSomething1()
            {
                //执行循环方法
                while (var != "aaa")
                {
                    //暂停10秒
                    System.Threading.Thread.Sleep(10000);
                }            BeginInvoke(new MethodInvoker(Finish));
            }
      

  6.   

    C# 中 第一句private Timers.Timer tmr; 编译未通过 原因:TimersVB.net 关于bar.Style的语句均不能通过是不是我的环境问题我用的VS.NET2003
      

  7.   

    为什么我的.net里没有progressBar1.Style这个
    有这个               progressBar1.StyleChanged唉
    现在感觉
    人啊,还是有点文化比较好啊
      

  8.   

    楼主你用的是vs.net2003啊!vs2003的滚动条是没有Marquee样子的.你有几种解决方案:
    1.升级到vs2005
    2.找第三方滚动条控件
    3.用一个循环滚动的Gif代替.
      

  9.   

    vs2003的滚动条是没有Marquee样子的那还可以实现WINDOWS开机时那个进度条的效果吗?我只能用vs.net2003这个环境,插件只能用SPEAD昨天看贴时有个系统函数是关于那种进度条的好像可以直接调用但是,看了之后不知道怎么用
      

  10.   

    vs2003自带的滚动条实现不了.除非你重写它.你可以找一个Gif动画来代替呀.这里有一个开源的进度条控件,你可下载来用:
    http://www.codeproject.com/KB/cpp/indeterminateprogressbar.aspx
      

  11.   

    'LZ可以照这个自己写个ProgressbarPublic Class MyProgressBar
        Inherits UserControl    Private mStart As Boolean    Public Property Start() As Boolean
            Get
                Return Me.Timer1.Enabled
            End Get
            Set(ByVal value As Boolean)
                Me.Timer1.Enabled = value
            End Set
        End Property    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Me.Refresh()
        End Sub    Private Sub MyProgressBar_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            If Me.DesignMode Then Exit Sub
            Static i As Integer
            If i < Me.Width Then i += 10 Else i = 0
            Dim rects() As Rectangle = New Rectangle() {New Rectangle(i, 1, 10, 16), New Rectangle(i + 15, 1, 10, 16), New Rectangle(i + 30, 1, 10, 16)}
            e.Graphics.FillRectangles(Brushes.Cyan, rects)
            e.Graphics.DrawRectangles(Pens.Blue, rects)
        End Sub    Public Sub New()
            InitializeComponent()
            Me.BorderStyle = Windows.Forms.BorderStyle.Fixed3D
            Me.SetStyle(ControlStyles.OptimizedDoubleBuffer Or ControlStyles.ResizeRedraw, True)
        End Sub    Private Sub MyProgressBar_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
            Me.Height = 20
        End SubEnd Class
    public class MyProgressBar : UserControl 

        
        private bool mStart; 
        
        public bool Start { 
            get { return this.Timer1.Enabled; } 
            set { this.Timer1.Enabled = value; } 
        } 
        
        private void Timer1_Tick(object sender, System.EventArgs e) 
        { 
            this.Refresh(); 
        } 
        
        private void MyProgressBar_Paint(object sender, System.Windows.Forms.PaintEventArgs e) 
        { 
            if (this.DesignMode) 
                return; // TODO: might not be correct. Was : Exit Sub         if (static_MyProgressBar_Paint_i < this.Width) 
                static_MyProgressBar_Paint_i += 10; 
            else 
                static_MyProgressBar_Paint_i = 0; 
            Rectangle[] rects = new Rectangle[] {new Rectangle(static_MyProgressBar_Paint_i, 1, 10, 16), new Rectangle(static_MyProgressBar_Paint_i + 15, 1, 10, 16), new Rectangle(static_MyProgressBar_Paint_i + 30, 1, 10, 16)}; 
            e.Graphics.FillRectangles(Brushes.Cyan, rects); 
            e.Graphics.DrawRectangles(Pens.Blue, rects); 
        } 
        static int static_MyProgressBar_Paint_i; 
        
        public MyProgressBar() 
        { 
            InitializeComponent(); 
            this.BorderStyle = Windows.Forms.BorderStyle.Fixed3D; 
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw, true); 
        } 
        
        private void MyProgressBar_Resize(object sender, System.EventArgs e) 
        { 
            this.Height = 20; 
        } 
        

    需要在usercontrol上添加一个timer1控件,internal设置成100即可。在winform上使用时通过设置 Me.MyProgressBar1.Start = True即可。 
      

  12.   

    public static void DriverProgressBar(int totalNum,int completeNum,System.Windows.Forms.ProgressBar progressBar)
    {
    progressBar.Value = (int)( (double)completeNum / (double)totalNum * 100 );
    }
      

  13.   

    Threading
    这个我不会用所以放进去也不好用急啊
      

  14.   

    System.Threading.Thread.Sleep(10 * 1000);原来这样就可以了
    哈哈
      

  15.   

    start = Now; 
    System.Threading.Thread th = new System.Threading.Thread(DoLoop);
    bar.Style = ProgressBarStyle.Blocks; 
    string str = string.Format("{0}:{1}:{2}", DateDiff(DateInterval.Hour, start, Now).ToString.PadLeft(2, "0"), DateDiff(DateInterval.Minute, start, Now).ToString.PadLeft(2, "0"), DateDiff(DateInterval.Second, start, Now).ToString.PadLeft(2, "0")); 
    this.Invoke(new RunModifyText(ModifyText), str); 以上均通不过编译
    我改不出来了
      

  16.   

    http://blog.csdn.net/liuyun1987/archive/2008/01/25/2065048.aspx
      

  17.   

    VS.net 2003
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
      

  18.   

    这人发广告来了
    删号啊版主我不懂C#.net啊
    着急也没有用非常感谢你们给我的答案但是VS。net2005的在2003上就不行有了2005的代码不能用和没有一样郁闷啊急
      

  19.   

    http://www.codeproject.com/KB/progress/ProperProgress.aspx你看看这个管用吗?
      

  20.   

    一般软件是向下兼容,不是向上兼容,VS2005比2003扩展了很多东西,我也是被逼无奈,换了VS2005。
    8过公司服务器上有安装文件,不费事,嘿嘿