How to Add Progress bar in windows application ?



Hello Friend’s
In this article I am going to explain how to add progress bar in windows application.
In web application, we have much option and having third party controls too. Like
RadControls , NetAdvantages controls etc..  But in windows application, there is no straight way to add progress bar as per my thinking.  In this article I am going to give one small example so you can implement into your application.

First Make one application and in one Form put below controls.
1.)    Button (on clicking button we display progress Bar)
2.)    Picture box  (option to display Loading Image)
3.)    Label (give appropriate message to user like Please wait….)


public partial class KetanProgressBar : Form
    {
        private BackgroundWorker bgworker;
        delegate void SetControlValueCallback(Control oControl, string propName, object propValue);
        public KetanProgressBar()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            bgworker = new BackgroundWorker();
            bgworker.WorkerSupportsCancellation = true;
            bgworker.WorkerReportsProgress = true;
            bgworker.DoWork += bwMC_DoWork;
            bgworker.ProgressChanged += bwMC_ProgressChanged;
            bgworker.RunWorkerCompleted += bwMC_RunWorkerCompleted;
        }
        private void bwMC_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            SetControlPropertyValue(pictureBox1, "Visible", true);
            SetControlPropertyValue(lblWait, "Visible", true);
        }
        private void bwMC_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if(!e.Cancelled)
            {
                SetControlPropertyValue(lblWait, "Visible", false);
                SetControlPropertyValue(pictureBox1, "Visible", false);
                MessageBox.Show("Action Completed", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        private void SetControlPropertyValue(Control oControl, string propName, object propValue)
        {
            if (oControl.InvokeRequired)
            {
                SetControlValueCallback d = new SetControlValueCallback(SetControlPropertyValue);
                oControl.Invoke(d, new object[] { oControl, propName, propValue });
            }
            else
            {
                Type t = oControl.GetType();
                PropertyInfo[] props = t.GetProperties();
                foreach (PropertyInfo p in props)
                {
                    if (p.Name.ToUpper() == propName.ToUpper())
                    {
                        p.SetValue(oControl, propValue, null);
                    }
                }
            }
        }
        private void bwMC_DoWork(object sender, DoWorkEventArgs e)
        {
            bgworker.ReportProgress(1);
//just for test we make looping so progressbar will display.
            for (int i = 0; i <= 100000000; i++)
            { }
           
        }
        private void button1_Click(object sender, EventArgs e)
        {
            bgworker.RunWorkerAsync();
            Application.DoEvents();
        }



I think all statements are self explanatory. There is no need to explain. If you have any doubt in any statement then let me know.

Enjoy !!!!!!

Posted in |

0 comments:

Post a Comment