Never underestimate the power of Passion!

Tuesday, 14 February 2017

On 22:57 by Vardan Kumar in    No comments
Progress bar

Progress Bar in C# Windows form application

Why Progress Bar is important?

Lets imagine ourselves as a user of an application. Now lets suppose our application is taking a minute or two to do its desired purpose. Meantime application is doing nothing, we are just seeing a dumb windows form in front of you, what would we think...Well yes we would think that the application hung up in about 20 to 30 sec or less and would try to interrupt the application progress either by closing it or opening task manager and what not?

Hence it is considered a good practice as a developer to tell our user the progress of our application. Hope we have understood the main purpose of a progress bar, apart from it makes our GUI look better.

Including a progress bar is not just enough, incorporating an efficient progress bar which should move continuously is also necessary as suppose if progress bar has stopped progressing for about 20-30 sec, again we as a user would think that application has stopped responding.

Also additionally,
  •  if we can tell the user what application is currently doing that would be nice.  

  • We will also try to increase the progress of the progress bar linearly or monotonously as far as possible which we can do by increasing its progress in a loop.
Progress bar C# windows form application
Progress Bar c#

Source Code

                    Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
                    Workbook xlWorkbook = ExcelApp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);

                    DataTableCollection collection = dataSet.Tables;

                    Sheets xlSheets = null;
                    Worksheet xlWorksheet = null;
                    //Create Excel Sheets
                    xlSheets = ExcelApp.Sheets;
                    xlWorksheet = (Worksheet)xlSheets.Add(xlSheets[1],
                                   Type.Missing, Type.Missing, Type.Missing);

                    System.Data.DataTable table = collection[0];
                    xlWorksheet.Name = table.TableName;
                    for (int j = 1; j < table.Columns.Count + 1; j++)
                        ExcelApp.Cells[1, j] = table.Columns[j - 1].ColumnName;
                    lbl_Cmpltd.Text = "Preparing Excel Sheet";
                    // Storing Each row and column value to excel sheet
                    for (int k = 0; k < table.Rows.Count; k++)

                        prg_TaskProgress.Value = prg_TaskProgress.Value + 1;

                        lbl_OnGng.Text = "Adding Rows("+k.ToString()+"/"+(table.Rows.Count-1).ToString()+")";
                        for (int l = 0; l < table.Columns.Count; l++)
                            ExcelApp.Cells[k + 2, l + 1] =

                    lbl_Cmpltd.Text = "Adding Rows";

Features and Logic

  • Above Source code populates an excel sheet via data tables in data set
  • In above code table.rows.Count gives the total number of rows and hence our loop runs the same number of times
  • k will take values from 0 to total number of rows in data table
  • Progress Bar's value is incremented by '1' each time the loop is executed.
  • Progress Bar's maximum value should be set in its properties and its value should not exceed that maximum value or else it will throw an exception.
  • We can also change the style of our progress bar,I prefer continuous style for elegant looks.
  • Providing labels for what currently our application is performing makes our application more sophisticated.


Post a comment