Here is one way that loads the spash form and kicks the worker and waits
till close, then starts the main form using Application.Run (new MyForm());
as normal. The worker calls private methods in the form that make sure the
calls are done on the forms thread. Using this pattern, you can update any
controls on your form.
using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Threadin g;
namespace CSharpExample.M yApp
{
/// <summary>
/// Summary description for SpashForm.
/// </summary>
public class SplashForm : System.Windows. Forms.Form
{
private System.Windows. Forms.Label label1;
private System.Windows. Forms.Label lblText;
private System.Windows. Forms.Button btnClose;
private System.Windows. Forms.ProgressB ar progressBar1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.Componen tModel.Containe r components = null;
public SplashForm()
{
//
// Required for Windows Form Designer support
//
InitializeCompo nent();
//
// TODO: Add any constructor code after InitializeCompo nent call
//
}
[STAThread]
static void Main()
{
SplashForm splashForm = new SplashForm();
splashForm.Show Dialog();
splashForm.Dial ogResult = DialogResult.OK ;
if ( splashForm.Dial ogResult == DialogResult.Ca ncel )
return;
Application.Run (new MyForm());
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Disp ose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.label1 = new System.Windows. Forms.Label();
this.lblText = new System.Windows. Forms.Label();
this.btnClose = new System.Windows. Forms.Button();
this.progressBa r1 = new System.Windows. Forms.ProgressB ar();
this.SuspendLay out();
//
// label1
//
this.label1.Fon t = new System.Drawing. Font("Microsoft Sans Serif", 9.75F,
System.Drawing. FontStyle.Bold, System.Drawing. GraphicsUnit.Po int,
((System.Byte)( 0)));
this.label1.Loc ation = new System.Drawing. Point(16, 16);
this.label1.Nam e = "label1";
this.label1.Siz e = new System.Drawing. Size(264, 23);
this.label1.Tab Index = 0;
this.label1.Tex t = "Welcome";
//
// lblText
//
this.lblText.Lo cation = new System.Drawing. Point(16, 56);
this.lblText.Na me = "lblText";
this.lblText.Si ze = new System.Drawing. Size(264, 23);
this.lblText.Ta bIndex = 1;
this.lblText.Te xt = "Loading XYZ...";
//
// btnClose
//
this.btnClose.L ocation = new System.Drawing. Point(208, 128);
this.btnClose.N ame = "btnClose";
this.btnClose.T abIndex = 2;
this.btnClose.T ext = "Cancel";
this.btnClose.C lick += new System.EventHan dler(this.btnCl ose_Click);
//
// progressBar1
//
this.progressBa r1.Location = new System.Drawing. Point(16, 96);
this.progressBa r1.Name = "progressBa r1";
this.progressBa r1.Size = new System.Drawing. Size(264, 23);
this.progressBa r1.Step = 1;
this.progressBa r1.TabIndex = 3;
//
// SplashForm
//
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(292, 159);
this.Controls.A dd(this.progres sBar1);
this.Controls.A dd(this.btnClos e);
this.Controls.A dd(this.lblText );
this.Controls.A dd(this.label1) ;
this.FormBorder Style = System.Windows. Forms.FormBorde rStyle.FixedDia log;
this.Name = "SplashForm ";
this.Text = "Loading MyApp...";
this.Load += new System.EventHan dler(this.Spash Form_Load);
this.ResumeLayo ut(false);
}
#endregion
private void SpashForm_Load( object sender, System.EventArg s e)
{
Thread t = new Thread(new ThreadStart(Wor kerStart));
t.IsBackground = true;
t.Start();
}
private delegate void UpdatePBarDeleg ate(int step);
private void UpdatePBar(int step)
{
if ( InvokeRequired )
{
UpdatePBarDeleg ate ud = new UpdatePBarDeleg ate(UpdatePBar) ;
Invoke(ud, new object[]{step});
}
else
{
this.progressBa r1.Value = this.progressBa r1.Value + step;
}
}
private void Done()
{
if ( InvokeRequired )
{
Invoke(new MethodInvoker(D one));
}
else
{
this.lblText.Te xt = "Done";
this.Close();
}
}
private void WorkerStart()
{
for(int i = 0; i < 100; i++)
{
this.UpdatePBar (1); // Update progress bar 1 step.
Thread.Sleep(50 ); // Fake some delay - your processing goes here.
}
this.Done(); // Do any other updates on the splash form and load main and
quit.
}
private void btnClose_Click( object sender, System.EventArg s e)
{
this.DialogResu lt = DialogResult.Ca ncel;
//this.Close();
}
}
}
--
William Stacey
MVP Directory Services
http://mvp.support.microsoft.com