Windows Develop Bookmark and Share   
 index > Windows Forms General > Use of Progressbar
 

Use of Progressbar

Hi All,

I am using VS03+C#+ Winforms.

In my application when user click submit button i want to use progressbar.

It should display just like when we use set up,like that progressbar progresses view.

Detail procedure in C# is appriciated.

Thanks in advance.

sunDAC  Thursday, June 05, 2008 10:08 AM
Hi,

You should get a book on writing code. Just a learn to code in 24 hours


Dont be confused by System.Threading.Thread.Sleep, all its doing is pausing the current thread (it isn't creating a multithreaded app).


I urge you to just use a Cursor.Wait and once the operation is finished set Cursor = Cursor.Default. Its simple and will look professional. Otherwise just show the picturebox, then execute your lengthy operation, then hide the picturebox,eg:



private void button1_Click(object sender, System.EventArgs e)

{


try

{

this.Cursor = Cursors.WaitCursor;


//Show the picture box

pictureBox1.Visible = true;


for (int counter = 0; counter < maximum; counter++)

{


//call the lengthy operation here, eg:

DoSomething(counter);


//You might need to do a this.Refresh and or DoEvents, to give the picturebox the chance to show/render the gif eg:

this.Refresh();

Application.DoEvents();

}

}

finally

{

this.Cursor = Cursors.Default;

pictureBox1.Visible = false;

}

}








Meaning Of Lights  Friday, June 06, 2008 6:13 AM
Hi San7481,

Kindly drag a Progress Bar onto your form. In code you will need to set the ProgressBar1.Value = xyz; depending on progress.

If your performing lengthy operation in code may I suggest changing the Cursor.Wait (google it)rather than a Progress Bar. Working at PSS I'm worried a progress bar control might a bit too complex for you.

HTH

Meaning Of Lights  Thursday, June 05, 2008 10:20 AM

Hi, Thanks for the reply.

By just only giving ProgressBar1.Value how it is possible ?

I cant understand. Can you please elabrate the point?

Waiting for the reply.

sunDAC  Thursday, June 05, 2008 12:26 PM

Hi san7481.

Here's a code example of using the progressbar. I put in a random number of loops to simulate various conditions but the code is very simple. And I agree, using a wait cursor is a very good idea. Note that I put it all in a Try block so that it ensures the cursor will return to the default -- always a good idea when messing with cursors.

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

Random rnd = new Random();

int maximum = rnd.Next(20, 100);

progressBar1.Value = 0;

progressBar1.Maximum = maximum;

progressBar1.Show();

try

{

this.Cursor = Cursors.WaitCursor;

for (int counter = 0; counter < maximum; counter++)

{

progressBar1.Value += 1;

System.Threading.Thread.Sleep(50);

}

}

finally

{

this.Cursor = Cursors.Default;

progressBar1.Hide();

}

}

}

Dig-Boy  Thursday, June 05, 2008 1:40 PM

Hi ,

Thanks for the reply.

I have used the code which you give me.

It works fine .

i have seen on one site during completion of progressbar the use of some animation,just like butterfly flying.

Is it possible while completion of progressbar to use some images along with it?

Do you have any idea?

If yes then suggest me some idea.

Your code is perfectly working.

Waiting for reply.

sunDAC  Thursday, June 05, 2008 2:34 PM

If you are not using any 3rd party controls for animation then you could get by with setting a GIF image into a picturebox when the progress bar is done. A multi-frame GIF will begin to animate when the image property is set for the picture box.

You can create a GIF through the open source photo editor the Gimp (www.gimp.org) -- you may also want to look into another open source free program Paint.Net (www.paintnet.org) to create the frames for Gimp.

Other than that maybe some other forum members have some ideas for you.

Dig-Boy  Thursday, June 05, 2008 3:12 PM

Hi,

Thanks for the reply.I have taken picturebox and as well assigned an animated gif file.

i want when i click on submit button then instead of progressbar the animated file which i assigned to

picturebox should display.

I have tried that but get stuck where to show and hide the picturebox.I have never tried threading before .

Can you please give me appropriate procedure ,where to hide and display the picturebox?

Waiting for the reply.

sunDAC  Friday, June 06, 2008 5:08 AM
G'day,

Kindly post the code that doesn't work eg: PictureBox1.Picture = "C:\Animated.Gif"; and we'll sort you out once and for all.


Cheers,
JEz
Meaning Of Lights  Friday, June 06, 2008 5:13 AM

Hi ,

Thanks for the reply . i have taken picturebox and assigned image using image property.

On form load picturebox is visible false.

Below is my code.

private void button1_Click(object sender, System.EventArgs e)

{

progressBar1.Hide();

Random rnd = new Random();

int maximum = rnd.Next(20, 100);

// progressBar1.Value = 0;

// progressBar1.Maximum = maximum;

// progressBar1.Show();

try

{

this.Cursor = Cursors.WaitCursor;

for (int counter = 0; counter < maximum; counter++)

{

// progressBar1.Value += 1;

System.Threading.Thread.Sleep(10);

pictureBox1.Visible = true;

}

}

finally

{

this.Cursor = Cursors.Default;

pictureBox1.Visible = false;

}

}

i have just visible picturebox visible where used progressbar in previous code.

I have done all trial and error.But due to the fact that i cant work on threading ,i realy get struck.

So where i put the picturbox appropriately.

Waiting for the reply.

sunDAC  Friday, June 06, 2008 6:01 AM
Hi,

You should get a book on writing code. Just a learn to code in 24 hours


Dont be confused by System.Threading.Thread.Sleep, all its doing is pausing the current thread (it isn't creating a multithreaded app).


I urge you to just use a Cursor.Wait and once the operation is finished set Cursor = Cursor.Default. Its simple and will look professional. Otherwise just show the picturebox, then execute your lengthy operation, then hide the picturebox,eg:



private void button1_Click(object sender, System.EventArgs e)

{


try

{

this.Cursor = Cursors.WaitCursor;


//Show the picture box

pictureBox1.Visible = true;


for (int counter = 0; counter < maximum; counter++)

{


//call the lengthy operation here, eg:

DoSomething(counter);


//You might need to do a this.Refresh and or DoEvents, to give the picturebox the chance to show/render the gif eg:

this.Refresh();

Application.DoEvents();

}

}

finally

{

this.Cursor = Cursors.Default;

pictureBox1.Visible = false;

}

}








Meaning Of Lights  Friday, June 06, 2008 6:13 AM

HI,

Thanks for the reply.I have gone through the code ,it works fine .

cheers !

sunDAC  Monday, June 09, 2008 4:39 AM

You can use google to search for other answers

Custom Search

More Threads

• want to shift user-control below another user-control to reveal invisible part.
• TextBox background color issue on transparent GroupBox
• axWindowsMediaPlayer 's network.bufferingTime set but error
• Is Label Text Ellipsed?
• Windows Forms Multi-Threading
• Forms
• Datagridviews on top of each other.
• UserControl.AutoSize
• how to add events to a button created at runtime? vb.net 2008
• How to detect if a node which is to be added is already present in nodes collection of a child node in a treeview in c#