Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
How to do progress bar in windows application?

In my windows application i have one login screen.
If ypu click on log in button then application will check the access using client context(share point object).
Application will take 30 sec to check the access.I wanna shoe progress bar while application checking the access
How to do this?
Do i need to use thread ?
Please help me..

this is the code am using in button click event.
C#
private void btnok_Click(object sender, EventArgs e)
{            
    try
    {              
        if (!String.IsNullOrEmpty(UserName.Text) & !String.IsNullOrEmpty(PassWord.Text))
        {                                                  
            validateuser( Username, s Password,  SiteURL,  Domain);
        }
        else
        {
            MessageBox.Show("Please fill  Usename and Password", "Add Items", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }              
    }
    catch (Exception )
    {
        MessageBox.Show("Error:  Unable to connect","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
    }
}


Solutions provided below are not working...:(
Posted
Updated 26-Nov-13 3:49am
v3

 
Share this answer
 
You need to do some ground study on showing Progressbar.

Step 1.You can have your own custom progress bar with information(Strings getting updated)
create Progress bar view
Step 2. nest the long running thread inside the Do Work delegate
C#
pd.worker.DoWork += delegate(object s, DoWorkEventArgs args)
               {

Step 3 : Hook up worked completed event
C#
pd.worker.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args)
               {
                   //Have your custom logic here
                   pd.AllowClosing = true;
                   pd.Close();
               };

Step 4: run the long running process as background thread and show the dialog
C#
if (!pd.worker.IsBusy)
                {
                    pd.worker.RunWorkerAsync();
                    pd.ShowDialog();
                }
 
Share this answer
 
Comments
Am Gayathri 26-Nov-13 6:55am    
What is pd here?
AnthonyMG 26-Nov-13 11:37am    
pd is the instance for your newly created progressbarview ( it can be the contorl in your case)
The way I would do it is to cheat, a little.
Create a form with no borders, title bar or anything else, and drop a ProgressBar onto it. Set the ProgressBar.Dock property to Fill. Size the form.
Set ProgressBar.Style to Marquee
Set the Form.StartPosition to CenterParent.
Set the Form.ShowInTaskbar to false.
Add a bool property SuccessfullValidate to the form.
Create a constructor that accepts the value you want to pass to validateuser
Handle the Form.Shown event:
C#
private void frmProgress_Shown(object sender, EventArgs e)
    {
    BackgroundWorker work = new BackgroundWorker();
    work.DoWork += new DoWorkEventHandler(work_DoWork);
    work.RunWorkerCompleted += new RunWorkerCompletedEventHandler(work_RunWorkerCompleted);
    work.RunWorkerAsync();
    }

void work_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
    Close();
    }

void work_DoWork(object sender, DoWorkEventArgs e)
    {
    try
       {
       validateuser( Username, s Password,  SiteURL,  Domain);
       SuccessfullValidate = true;
       }
    catch
       {
        MessageBox.Show("Error:  Unable to connect", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
       SuccessfullValidate = false;
       }
    }
Then all you have to do is create an instance, call ShowDialog and check SuccessfullValidate after it returns.
 
Share this answer
 
Comments
Am Gayathri 26-Nov-13 6:56am    
Where is the progress bar here?
OriginalGriff 26-Nov-13 7:44am    
Read what I said:
"Create a form with no borders, title bar or anything else, and drop a ProgressBar onto it.
Set the ProgressBar.Dock property to Fill. Size the form.
Set ProgressBar.Style to Marquee"
When you display the form, it shows the progress bar while the operation occurs, then removes it itself when it completes.
Call the function using

C#
System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(FunctionName));
                thread.Start();

private void FunctionName()
{
    EnableProgressBar(true);
    //...
    //...
    EnableProgressBar(false);
}

internal delegate void SetProgressBar(bool Value);
        private void EnableProgressBar(bool Value)
        {
            if (this.InvokeRequired)
                this.Invoke(new SetProgressBar(EnableProgressBar), Value);
            else
                ProgressBar1.Visible = Value;
        }
 
Share this answer
 
use a timer and set it's interval 1000 (1 sec) and on timer click write
progressbar.Value = progressbar.Value + 1;
before this dont forget to write
progressbar.MaxValue=30;
progressbar.Value=0;
 
Share this answer
 
Comments
OriginalGriff 25-Nov-13 5:31am    
Reason for my vote of one: It's pretty clear from the question that the OPs validateuser method is asynchronous - it doesn't return until is succeeds or fails. So it is a blocking method - and Timer.Tick events will not be honoured until after the method exits if this is called on the UI thread, which the code makes abundantly clear it is!
Your solution will not work: the progress bar will not get updated until teh user no longer needs to see it...

Please, think about the question before replying in future?
agent_kruger 25-Nov-13 6:03am    
sorry and thank you to correct me!!
OriginalGriff 25-Nov-13 6:56am    
No problem!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900