PDA

Click to See Complete Forum and Search --> : Splash screen


pjrage
Apr 25th, 2007, 07:27 AM
I want to add a simple splash screen to a program I'm making because the main form takes several second to load it's controls.

To do this I was thinking just two forms, one is the splash, one is the main form. Form1 will be the main, Form2 the splash. I can get the program to start up on Form2, and I can get Form2 to open Form1, but I can't get Form1 to close Form2! At least not without error or closing itself or some other issue.

Any ideas?

vbNeo
Apr 25th, 2007, 07:31 AM
Start the app with form one, have it load form2(splash) first thing, do your loading, when done, dispose of the form2 object.

Hack
Apr 25th, 2007, 07:42 AM
but I can't get Form1 to close Form2! At least not without error or closing itself or some other issue.That seems very odd.

First, what error do you get?

Second, what are the "other issues" that happen?

pjrage
Apr 25th, 2007, 08:27 AM
Well, what Neo said does not work because what is taking a while to load is the activex controls on Form1 themselves. I created Form2 (the splash), and in the Form1 Load event, the first two lines were:

Form splash = new Form2();
splash.show();

The problem is that it didn't show the splash until Form1 was loaded, which, to load the controls I'm using, can take like 20 seconds, hence, the need for a splash screen.

I really just don't know where to put the loading and unloading of Form1 and Form2.

If I load and show Form2 before initializecomponent in Form1, I can get it to show up ~ 30% earlier, but that is still 70% too late.

If I start the program with Form2, it shows up right away, and I'm playing with where then do I load Form1, and then at what point (and how do I access the instance of Form2 from Form1 once Form1 is loaded) do I hide Form2?

I figured this was a really simple question that I just didn't have the knowledge to answer, but is it actually more complicated?

pjrage
Apr 25th, 2007, 08:39 AM
Ok, I think I just found something that works, but is it right?

I first changed it to load the program with Form2 (the splash). Then, under the Form2.Shown event, I added:

Form mainform = new Form1();
mainform.Show();
this.Hide();

This worked great, it shows the splash right away, opens my main form (actually faster than ever?!) and hides the splash basically at the same time that Form1 actually comes up displayed. Exactly the result I wanted.

But this also brings about a new question for me... is it possible to *switch* the "main instance" (for lack of a better name since I don't know the proper name) to Form1 once the splash is done? i.e., so when you close Form1, it properly and completely closes the app? (Without having to use the Closing/Closed events to close Form2 simultaneously). And if not, what would be the "proper" way to shut down the program using these events? How can I access the instance of Form2 to dispose of it from the .Closing or .Closed events of Form1?

pjrage
Apr 25th, 2007, 01:07 PM
To get Form1 (main form) to close Form2 (splash screen, but also "host" form or "main" form or whatever you call the form that the program starts), I followed some code from another link regarding storing an instance variable.

I made a property inside of Form1 called InstanceRef:

private Form m_InstanceRef = null;
public Form InstanceRef
{
get { return m_InstanceRef;}
set { m_InstanceRef = value;}
}

Then, where Form2 opens Form1, I changed it to look like this:

Form1 mainform = new Form1();
mainform.InstanceRef = this;
mainform.Show();
this.Hide();

Then, in Form1.FormClosed event, I did:

//Shutdown all timers
tim1.Enabled = false;
tim2.Enabled = false;

//Close forms
this.InstanceRef.Dispose();
this.Dispose();

This closes the whole app, and seems to work OK.

Two questions, though, first, after I dispose the "main" form, should I do a this.Dispose() like I did? Since the first .Dispose() is closing the main form, I think it is thereby closing the form that the this.Dipose() is trying to dispose? Will/can this cause an error to do this, or is an OK "backup plan?"

The second question is, overall, is the the "right" way to do this, and if not, is there a better/simpler/easier/preferred way to do it?

(edited for a code mistype)

pjrage
Apr 26th, 2007, 08:23 AM
Anyone? Is that the "right way?" Or is there a better way?

jmcilhinney
Apr 29th, 2007, 12:06 AM
http://www.codeproject.com/csharp/PrettyGoodSplashScreen.asp

Fromethius
Apr 29th, 2007, 10:01 PM
I'm pretty sure you would need to use multi-threading. One thread loads your application while the other thread displays the splash screen.