Csharp/C Sharp/GUI Windows Form/ProgressBar
ProgressBar Host
<source lang="csharp"> /* User Interfaces in C#: Windows Forms and Custom Controls by Matthew MacDonald Publisher: Apress ISBN: 1590590457
- /
using System.Drawing; using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; namespace ProgressBarHost {
/// <summary> /// Summary description for ProgressBarHost. /// </summary> public class ProgressBarHost : System.Windows.Forms.Form { internal System.Windows.Forms.Timer tmrIncrementBar; private System.Windows.Forms.Button cmdStart; private Progress status; private System.ruponentModel.IContainer components; public ProgressBarHost() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.ruponents = new System.ruponentModel.Container(); this.status = new Progress(); this.tmrIncrementBar = new System.Windows.Forms.Timer(this.ruponents); this.cmdStart = new System.Windows.Forms.Button(); this.SuspendLayout(); // // status // this.status.Location = new System.Drawing.Point(12, 8); this.status.Name = "status"; this.status.Size = new System.Drawing.Size(272, 88); this.status.TabIndex = 0; // // tmrIncrementBar // this.tmrIncrementBar.Interval = 1000; this.tmrIncrementBar.Tick += new System.EventHandler(this.tmrIncrementBar_Tick); // // cmdStart // this.cmdStart.Location = new System.Drawing.Point(88, 152); this.cmdStart.Name = "cmdStart"; this.cmdStart.Size = new System.Drawing.Size(92, 24); this.cmdStart.TabIndex = 1; this.cmdStart.Text = "Start"; this.cmdStart.Click += new System.EventHandler(this.cmdStart_Click); // // ProgressBarHost // this.AutoScaleBaseSize = new System.Drawing.Size(5, 14); this.ClientSize = new System.Drawing.Size(292, 194); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.cmdStart, this.status}); this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.Name = "ProgressBarHost"; this.Text = "ProgressBarHost"; this.ResumeLayout(false); } #endregion /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new ProgressBarHost()); } private void tmrIncrementBar_Tick(object sender, System.EventArgs e) { status.PerformStep(); if (status.Maximum == status.Value) { tmrIncrementBar.Enabled = false; } } private void cmdStart_Click(object sender, System.EventArgs e) { tmrIncrementBar.Enabled = false; status.Value = 0; status.Maximum = 20; status.Step = 1; tmrIncrementBar.Enabled = true; } } /// <summary> /// Summary description for Progress. /// </summary> public class Progress : System.Windows.Forms.UserControl { internal System.Windows.Forms.Label lblProgress; internal System.Windows.Forms.ProgressBar Bar; /// <summary> /// Required designer variable. /// </summary> private System.ruponentModel.Container components = null; public Progress() { // This call is required by the Windows.Forms Form Designer. InitializeComponent(); // TODO: Add any initialization after the InitForm call } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Component Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.lblProgress = new System.Windows.Forms.Label(); this.Bar = new System.Windows.Forms.ProgressBar(); this.SuspendLayout(); // // lblProgress // this.lblProgress.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right); this.lblProgress.Location = new System.Drawing.Point(5, 46); this.lblProgress.Name = "lblProgress"; this.lblProgress.Size = new System.Drawing.Size(152, 16); this.lblProgress.TabIndex = 3; this.lblProgress.Text = "0% Done"; this.lblProgress.TextAlign = System.Drawing.ContentAlignment.TopCenter; // // Bar // this.Bar.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right); this.Bar.Location = new System.Drawing.Point(5, 6); this.Bar.Name = "Bar"; this.Bar.Size = new System.Drawing.Size(154, 32); this.Bar.TabIndex = 2; // // Progress // this.Controls.AddRange(new System.Windows.Forms.Control[] { this.lblProgress, this.Bar}); this.Name = "Progress"; this.Size = new System.Drawing.Size(164, 68); this.ResumeLayout(false); } #endregion [Description("The current value (between 0 and Maximum) which sets the position of the progress bar"), Category("Behavior"), DefaultValue(0)] public int Value { get { return Bar.Value; } set { Bar.Value = value; UpdateLabel(); } } public int Maximum { get { return Bar.Maximum; } set { Bar.Maximum = value; } } public int Step { get { return Bar.Step; } set { Bar.Step = value; } } public void PerformStep() { Bar.PerformStep(); UpdateLabel(); } private void UpdateLabel() { lblProgress.Text = (Math.Round((decimal)(Bar.Value * 100) / Bar.Maximum)).ToString(); lblProgress.Text += "% Done"; } }
}
</source>
ProgressBar in C#
<source lang="csharp"> // I am trying to use Class ProgressBar in C# // Editor Used : Antechinus http://www.c-point.ru // Dt. 9th Jan. 2001 // ... Special thanks Saurabh for help & guidance ... Thanks! someday i would // do the way you do.... programming // ... thanks Mahesh for loading my codes. Your site rules! // ... thanks to you for reading it you know better who you are :) // if you have any comments or doubts or whatever please don"t // hesitate to e-mail me.
using System; using System.Drawing; using System.ruponentModel; using System.Windows.Forms; /// <summary> /// Summary description for Win32Form2. /// </summary> public class Win32Form2 : System.Windows.Forms.Form { /// <summary> /// Required by the Win Forms designer /// </summary> private System.ruponentModel.Container components; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Label label1; private System.Windows.Forms.Button button1; private System.Windows.Forms.ProgressBar progressBar1; public Win32Form2() { // Required for Win Form Designer support InitializeComponent(); } /// <summary> /// The main entry point for the application. /// </summary> public static void Main(string[] args) { Application.Run(new Win32Form2()); } /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with an editor /// </summary> private void InitializeComponent() { this.ruponents = new System.ruponentModel.Container(); this.label1 = new System.Windows.Forms.Label(); this.progressBar1 = new System.Windows.Forms.ProgressBar(); this.button1 = new System.Windows.Forms.Button(); this.textBox1 = new System.Windows.Forms.TextBox(); //@design this.TrayHeight = 0; //@design this.TrayLargeIcon = false; //@design this.TrayAutoArrange = true; label1.Location = new System.Drawing.Point(32, 40); label1.Text = "Progress Value"; label1.Size = new System.Drawing.Size(88, 24); label1.TabIndex = 2; progressBar1.Maximum = 10; progressBar1.Location = new System.Drawing.Point(8, 312); progressBar1.Minimum = 0; progressBar1.TabIndex = 0; progressBar1.Value = 0; //We have calculated the excat size which will result in only 20 boxes to be drawn progressBar1.Size = new System.Drawing.Size(520, 40); progressBar1.Step = 1; button1.Location = new System.Drawing.Point(152, 168); button1.Size = new System.Drawing.Size(144, 48); button1.TabIndex = 1; button1.Text = "button1"; button1.Click += new System.EventHandler(button1_Click); textBox1.Location = new System.Drawing.Point(136, 40); textBox1.Text = "0"; textBox1.TabIndex = 3; textBox1.Size = new System.Drawing.Size(184, 20); this.Text = "Win32Form2"; this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(616, 393); this.Click += new System.EventHandler(Win32Form2_Click); this.Controls.Add(textBox1); this.Controls.Add(label1); this.Controls.Add(button1); this.Controls.Add(progressBar1); } protected void button1_Click(object sender, System.EventArgs e) { //this checking is automatically done as stated in the Ref Documentation //but it does not work , BUGssssss //so we have to do it shhhhh .... if (progressBar1.Value == progressBar1.Maximum){ progressBar1.Value = progressBar1.Minimum; } progressBar1.PerformStep(); textBox1.Text=progressBar1.Value.ToString() ; // Displays the values of progressbar in textbox } protected void Win32Form2_Click(object sender, System.EventArgs e) { } }
</source>