You are on page 1of 15

ProgressBar in C#

// // // // // // // // //

I am trying to use Class ProgressBar in C# Editor Used : Antechinus http://www.c-point.com 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 using using using System; System.Drawing; System.ComponentModel; 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.ComponentModel.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.components = new System.ComponentModel.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 Docume ntation //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 val ues of progressbar in textbox } protected void Win32Form2_Click(object sender, System.EventArgs e) { } }

/* User Interfaces in C#: Windows Forms and Custom Controls by Matthew MacDonald Publisher: Apress ISBN: 1590590457 */ using using using using using using using System.Drawing; System; System.Drawing; System.Collections; System.ComponentModel; System.Windows.Forms; 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.ComponentModel.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.components = new System.ComponentModel.Container(); this.status = new Progress(); this.tmrIncrementBar = new System.Windows.Forms.Timer(this.compon ents); 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.tmrIncr ementBar_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_Clic k); //

// 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[] { thi s.cmdStart, thi s.status}); this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawi ng.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.ComponentModel.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.TopC enter; // // Bar //

this.Bar.Anchor = ((System.Windows.Forms.AnchorStyles.Top | Syste m.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[] { thi s.lblProgress, thi s.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 th e 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"; } }

how to use progressbar control in windows application using c#.net


Posted by Suresh Dasari Thursday, December 23, 2010 Labels: C#.Net, Progressbar, Windows Application
Introduction Here I will explain how to use progressbar control in windows application using c#.net. Description I have done one windows application in that I have one form like creating user and assigning permission to user during at the time of user creation and displaying that result in one data gridview for this process its taking time at that time user dont know what is happening after button click for that reason I have used progressbar to give hint like processing. Previously I have implemented progress bar during postbacks in asp.net check that article here how to show progressbar during postbacks in asp.net . We can specify different styles to progressbar we have three types of styles are available. 1. 1) Blocks 2. 2) Continuous 3. 3) Marquee Blocks This property Indicates progress by increasing the number of segmented blocks in a ProgressBar.

Continuous This property Indicates progress by increasing the size of a smooth, continuous bar in a ProgressBar.

Marquee This Property indicates progress by continuously scrolling a block across a ProgressBar in a marquee fashion.

To implement progressbar in windows applications follow these steps. 1) First open visual studio 2) Select New --> Projects --> Select Windows Form Application --> Give Name for that project 3) Now one form will open. Open Toolbox in that select progressbar control and drag and drop it on your form same drag one button. Here my requirement is if I click on button I need to register user and assign roles and showing result in this mean time I need to show the progressbar for that I have written following code in button click This example shows progressbar during registration of all users private void btnCreate_Click(object sender, EventArgs e) { // Set Minimum to 1 to represent the first file being copied. Progressbar1.Minimum = 1; // Set Maximum to the total number of Users created. Progressbar1.Maximum = dt.Rows.Count; // Set the initial value of the ProgressBar. Progressbar1.Value = 1; // Set the Step property to a value of 1 to represent each user is being created. Progressbar1.Step = 1; // Loop through all files to copy. for (int i = 1; i <= dt.Rows.Count; i++) { bool result=InsertUserdetails(Username); // Copy the file and increment the ProgressBar if successful. if(result== true) {

// Perform the increment on the ProgressBar. Progressbar1.PerformStep(); } } } Here I will explain clearly what each step says Progressbar1.Minimum = 1; Progressbar1.Maximum = dt.Rows.Count; Here Minimum and Maximum properties define the range of values that means how much time that progressbar we need display. Minimum property is basically set to a value 0 Maximum property is typically set to value of indicating the completion of task. After that Progressbar1.Value = 1; This value property determines when to display the nextblock(Blocks) or increase the size of bar (Continuous). Progressbar1.Step = 1; Set the Step property to a value of 1 to represent each user is being created. Progressbar1.PerformStep(); This method will increment the value of the ProgressBar as each user registered. After completion of user registration progress bar shows fully loaded. I hope it helps you

his source code shows how to use the Timer, ProgressBar and StatusBar Windows Forms controls. The application looks like following:

VS .NET provides many Windows and Web forms controls. In this article, we demonstrate how to use Timer, ProgressBar and Statusbar Windows Forms controls in an application by using C#. Timer Control First create a Windows application. We add a timer control by dragging and draping the control on the form from the toolbox, the system will automatically add the necessary code. After that we set timer control properties as Interval = 1000 (1 sec) and Enabled as true. Setting the Enabled property to true starts the timer control. this.Clock.Enabled = true;

this.Clock.Interval = 1000; We also add the Tick event handler by double clicking on the timer control. this.Clock.Tick += new System.EventHandler(this.Timer_Tick); When the form is loaded and the timer control starts ticking, the Timer_Tick method is invoked. The method will get the hours, minutes, seconds by calling the GetTime() method. public void Timer_Tick(object sender,EventArgs eArgs) { if(sender==Clock) { lbTime.Text=GetTime(); } public string GetTime() { string TimeInString=""; int hour=DateTime.Now.Hour; int min=DateTime.Now.Minute; int sec=DateTime.Now.Second; TimeInString=(hour < 10)?"0" + hour.ToString() :hour.ToString(); TimeInString+=":" + ((min<10)?"0" + min.ToString() :min.ToString()); TimeInString+=":" + ((sec<10)?"0" + sec.ToString() :sec.ToString()); return TimeInString; } Status Bar Use the toolbox to draw a status bar. It will go to the bottom of the window to show the current status of the operation. You may initialize the status bar by giving some appropriate words to the text property of the status bar. this.statusBar1.Location = new System.Drawing.Point(0, 273); this.statusBar1.Name = "statusBar1"; this.statusBar1.Size = new System.Drawing.Size(416, 20); this.statusBar1.TabIndex = 4; this.statusBar1.Text = "Ready"; Status bar is used to give users information about the current status. For example when some action is taken, it may be used to tell the user that the system is done with the operation. So the user will be aware of what has happened. In this program we tried to load data from the remote server. It may take some time to grab the data from the server. The status bar is used to tell the user what the system is doing by setting the text property of the statusBar as follows statusBar1.Text="Connecting to DB..."; When the system is done with the loading, we should tell the user that the data have been loaded. Then the user is aware that he or she can do something else. statusBar1.Text ="Database loaded";

Progress Bar Progress bars are used to display the progress of your application or background tasks. There are three members of the ProgressBar class you should know about: the Maximum, the Minimum, and the Value properties. After creating instance of a progress bar you set the range of the progress bar by using Minimum and Maximum properties of the ProgressBar. The Step property is used to set number of steps in a progress bar. The Value property is used to set the current value of the progress bar. this.progressBar1.Location = new System.Drawing.Point(88, 232); this.progressBar1.Name = "progressBar1"; this.progressBar1.TabIndex = 0; this.progressBar1 .Maximum =10000; this.progressBar1 .Minimum =1; this.progressBar1 .Step =1; The following codes will make the progressbar increase the step one per time. for(int i=progressBar1.Minimum; i<=progressBar1.Maximum; i++) { progressBar1.PerformStep(); }

You might also like