Alex Rivera | Logout

How to display a "loading" overlay on windows forms while the form is loading its controls (or updating them)?

Asked 2010-09-16T06:36:57.737
21

I'm looking for an effective way to notify the user that a given form is currently loading (or updating) it's UI and it will take few seconds.

This may occurs at initial load or at update. Since it's very intensive and modifying ui controls, this has to be done on the ui thread, and therefore blocking the user.

Changing the cursor is not enought, I want to get a similar effect than on ajax pages, with the whole area that is overlayed by a semi transparent panel with an animated gear on the center.

Have you done something like that already? Or do you know interesting websites I should consult?

Thanks a lot

Edit
Report

1 Answer

4

Note that Winforms does not allow child controls to actually be transparent. As others have posted a separate transparent window is possible - but messy to manage.

Cheap way

  • drag all controls into one panel and make it the size of the window (easy change)
  • When operating: Hide that panel. Use the panels .DrawToBitmap method to set form background image.
  • show progress bar, do work with 'doevents,' hide it.
  • clear background image, re-show panel.

A Better way:

Progress Class - Consumer

I will give you a Usercontrol I wrote and have used in many different programs that does exactly what you want. Here is a trivial consumer example, you can paste into a form's code (yes, it just makes a bunch of new buttons for no reason):

Public Class Form1

    Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        End ''// use a flag if you would like a more graceful way to handle this. 
    End Sub

    WithEvents ucProgress As New Progress   ''// just doing it this way so I don''//t have to paste designer code.

    Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        Controls.Clear()
        Controls.Add(ucProgress)
        Me.ucProgress.pb.Visible = False
        ucProgress.StartProgress()
        Try
            ucProgress.Message = "Starting up..."
            Application.DoEvents()
            Me.ucProgress.pb.Visible = True
            Me.ucProgress.pb.Maximum = 21
            Me.ucProgress.pb.Value = 0

            For i As Integer = 0 To 20
                Dim btn As New Button

                btn.Top = +i * 3
                btn.Left = i * 8
                btn.Text = CStr(i)
                btn.Enabled = False ''// ONLY HAVE TO DO FOR CTLS RIGHT ON MAIN FORM
                ucProgress.EnabledStates.Add(btn, True)  ''// ONLY HAVE TO DO FOR C
answered 2010-09-21T19:11:13.470

Your Answer