C# WinForm控件TrackBar与ProgressBar及Timer的用法

时间:2023-01-30 22:15:23

 

源码下载:http://www.0379zd.com/news/show/26100.htm

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  Form1 : Form
    {
        
public  Form1()
        {
            InitializeComponent();
        }

        
private   void  button1_Click( object  sender, EventArgs e)
        {
            
if  (timer1.Enabled  ==   true )
            {
                timer1.Enabled 
=   false ;
                button1.Text 
=   " 开始 " ;
            }
            
else
            {
                timer1.Enabled 
=   true ;
                button1.Text 
=   " 停止 " ;
            }
        }

        
private   void  trackBar1_Scroll( object  sender, EventArgs e)
        {
            
// 使用trackBar1调整步进速度,不能为零
            timer1.Interval  =  Convert.ToInt16( 5000   /  trackBar1.Value);
        }

        
private   void  timer1_Tick( object  sender, EventArgs e)
        {
            
if  ( this .progressBar1.Value  ==   this .progressBar1.Maximum)
            {
                
this .progressBar1.Value  =   this .progressBar1.Minimum;
            }
            
else
            {
                
// 主要是这个函数,步进
                 this .progressBar1.PerformStep();
            }
            
// 计算百分比
             int  intPercent;
            intPercent 
=   100   *  ( this .progressBar1.Value  -   this .progressBar1.Minimum)  /  ( this .progressBar1.Maximum  -   this .progressBar1.Minimum);
            label1.Text 
=  Convert.ToInt16(intPercent).ToString()  +   " % " ;
        }

        
private   void  Form1_Load( object  sender, EventArgs e)
        {
            
this .progressBar1.Maximum  =   1000 ;
            
this .progressBar1.Step  =   10 ;
        }
    }
}