English 中文(简体)
Is there any way to load all UI controls before displaying? C# winform
原标题:

I have a complicate win form with lots of controls and bulky repaints, controls resizeing and positioning according to user screen, this causes the form to be shown while doing some rendering and repainting.

Is there any way to load UI and prepare it before displaying??I mean showing final UI after the whole repainting events done.

If using splash screen, before loading the main form, how should I do that??

Thanks

问题回答

Perhaps using SuspendLayout() and ResumeLayout() would work.

From MSDN:

The SuspendLayout and ResumeLayout methods are used in tandem to suppress multiple Layout events while you adjust multiple attributes of the control. For example, you would typically call the SuspendLayout method, then set the Size, Location, Anchor, or Dock properties of the control, and then call the ResumeLayout method to enable the changes to take effect.

@Kevin has the right idea and it s the approach I would use:

  1. Drop a Panel on your form and set Dock to Fill so that it covers the entire form.
  2. Drop a ProgressBar into the center of the panel with the Style property set to Marquee. This will make it scroll constantly.
  3. Now, at the end of your form s constructor (or Load event handler, if you are using it), call the SendToBack() method of your panel to expose the fully rendered controls.

You could create a "Loading" panel and cover your entire form with it temporarily while things load. You could maybe try calling PerformLayout on your layout managers prior to calling Form.Show to get them to front load the layout task.

A splash screen is easy enough. You can just show a topmost form and close it when your main form is done being shown, perhaps in the Activated event.

in your main:

    MyMainForm hostInterface;

    static void Main( )
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        hostInterface = new HostInterface();
        // do some stuff to get the form ready
        // here you can optionally instance a splash screen
        MySplashScreen splash;
        hostInterface.Load += splash.Close();    // <- might not be 100% accurate
        splash = new MySplashScreen();


        Application.Run(hostInterface);
    }

See how that works for you. The difference is that you are instantiating the form prior to running anything. I threw in an option for instancing a splash screen.

Compare the above to the standard auto-generated code:

static void Main( )
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Application.Run(new MyMainForm());
        }

Even without adding a splash screen, I have found that declaring the MyMainForm myform = new MyMainForm() has been better at loading all the controls prior to Application.Run(new MyMainForm())





相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签