Progress indicator in C# Winforms

2019-04-16 01:14发布

问题:

I have created some basic applications using windows forms C#.

What I am trying to achieve is that I have some task taking place inside a function. While executing that task I need to display a message box (with no buttons) with the text "Configuring...". I also need to blink this text. How can I do that?

Do I need to have another form for this? After completing this task this form needs to be hidden or closed?

I have googled this but couldn't see an answer, may be because of my unclear question in google.

回答1:

I would use a ProgresBar. Therefore you need to do your work in a background worker. An example what I mean you can find here:

https://stackoverflow.com/a/12127025/2243584

If you want to have some Text on the ProgressBar you need to create your custom ProgressBar. A complete sample is available here:

https://stackoverflow.com/a/3529945/2243584



回答2:

If you really need a progress indicator you have to do as toATwork said, and do a Background worker However, it is not a really async task, so you might find it hard to make it work properly

If you finally don't care about the message, and just need to show the users that something is happening, you can always use:

// To start
Cursor cursor = Cursor.Current;
Cursor.Current = Cursors.WaitCursor;

// To finish
Cursor.Current = Cursors.Default;

This will just put the mouse cursor in "loading", but it might work for you



回答3:

You can use a label on your form and change its text in function Body.

{
//your function body
label.Text="Configuring";
}