⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 Csharp实例编程百例.rar
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace testProgressBarApp
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Timer timer1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.ProgressBar MyProgressBar;
		private System.Windows.Forms.TrackBar trackBarSpeed;
		private System.Windows.Forms.Button btnStartStop;
		private System.Windows.Forms.Label lbFinishedPercent;
		private System.ComponentModel.IContainer components;

		public Form1()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			this.MyProgressBar = new System.Windows.Forms.ProgressBar();
			this.trackBarSpeed = new System.Windows.Forms.TrackBar();
			this.timer1 = new System.Windows.Forms.Timer(this.components);
			this.btnStartStop = new System.Windows.Forms.Button();
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.lbFinishedPercent = new System.Windows.Forms.Label();
			((System.ComponentModel.ISupportInitialize)(this.trackBarSpeed)).BeginInit();
			this.SuspendLayout();
			// 
			// MyProgressBar
			// 
			this.MyProgressBar.Location = new System.Drawing.Point(16, 24);
			this.MyProgressBar.Name = "MyProgressBar";
			this.MyProgressBar.Size = new System.Drawing.Size(296, 23);
			this.MyProgressBar.Step = 1;
			this.MyProgressBar.TabIndex = 0;
			this.MyProgressBar.Value = 1;
			this.MyProgressBar.Click += new System.EventHandler(this.progressBar1_Click);
			// 
			// trackBarSpeed
			// 
			this.trackBarSpeed.Location = new System.Drawing.Point(0, 152);
			this.trackBarSpeed.Minimum = 1;
			this.trackBarSpeed.Name = "trackBarSpeed";
			this.trackBarSpeed.Size = new System.Drawing.Size(104, 45);
			this.trackBarSpeed.TabIndex = 1;
			this.trackBarSpeed.Value = 1;
			this.trackBarSpeed.Scroll += new System.EventHandler(this.trackBarSpeed_Scroll);
			// 
			// timer1
			// 
			this.timer1.Interval = 1000;
			this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
			// 
			// btnStartStop
			// 
			this.btnStartStop.Location = new System.Drawing.Point(240, 144);
			this.btnStartStop.Name = "btnStartStop";
			this.btnStartStop.TabIndex = 2;
			this.btnStartStop.Text = "开始";
			this.btnStartStop.Click += new System.EventHandler(this.btnStartStop_Click);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 120);
			this.label1.Name = "label1";
			this.label1.TabIndex = 3;
			this.label1.Text = "设置速度";
			this.label1.Click += new System.EventHandler(this.label1_Click);
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(80, 64);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(56, 23);
			this.label2.TabIndex = 4;
			this.label2.Text = "已完成:";
			// 
			// lbFinishedPercent
			// 
			this.lbFinishedPercent.Location = new System.Drawing.Point(152, 64);
			this.lbFinishedPercent.Name = "lbFinishedPercent";
			this.lbFinishedPercent.TabIndex = 5;
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(336, 197);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.lbFinishedPercent,
																		  this.label2,
																		  this.label1,
																		  this.btnStartStop,
																		  this.trackBarSpeed,
																		  this.MyProgressBar});
			this.Name = "Form1";
			this.Text = "testProgressBar";
			this.Load += new System.EventHandler(this.Form1_Load);
			((System.ComponentModel.ISupportInitialize)(this.trackBarSpeed)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void progressBar1_Click(object sender, System.EventArgs e)
		{
		
		}

		private void timer1_Tick(object sender, System.EventArgs e)
		{
			if ( MyProgressBar.Value == MyProgressBar.Maximum) {
				MyProgressBar.Value = MyProgressBar.Minimum ;
			}else{
				MyProgressBar.PerformStep ();
			}
			int FinishedPercent;
			FinishedPercent = 100 * ( MyProgressBar.Value - MyProgressBar.Minimum )
							/(MyProgressBar.Maximum - MyProgressBar.Minimum );
			lbFinishedPercent.Text = Convert.ToInt16 (FinishedPercent).ToString() + "%";

		}

		private void trackBarSpeed_Scroll(object sender, System.EventArgs e)
		{
			timer1.Interval = Convert.ToInt16 (1000 / trackBarSpeed.Value) ;
		}

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

		private void Form1_Load(object sender, System.EventArgs e)
		{
		
		}

		private void label1_Click(object sender, System.EventArgs e)
		{
		
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -