Results 1 to 9 of 9

Thread: Splash screen

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2004
    Posts
    185

    Splash screen

    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?

  2. #2
    Frenzied Member vbNeo's Avatar
    Join Date
    May 2002
    Location
    Jutland, Denmark
    Posts
    1,994

    Re: Splash screen

    Start the app with form one, have it load form2(splash) first thing, do your loading, when done, dispose of the form2 object.
    "Lies, sanctions, and cruise missiles have never created a free and just society. Only everyday people can do that."
    - Zack de la Rocha


    Hear me roar.

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Splash screen

    Quote Originally Posted by pjrage
    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?

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Sep 2004
    Posts
    185

    Re: Splash screen

    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:

    C# Code:
    1. Form splash = new Form2();
    2. 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?

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Sep 2004
    Posts
    185

    Re: Splash screen

    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:

    C# Code:
    1. Form mainform = new Form1();
    2. mainform.Show();
    3. 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?

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Sep 2004
    Posts
    185

    Re: Splash screen

    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:

    C# Code:
    1. private Form m_InstanceRef = null;
    2. public Form InstanceRef
    3. {
    4.   get { return m_InstanceRef;}
    5.   set { m_InstanceRef = value;}
    6. }

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

    C# Code:
    1. Form1 mainform = new Form1();
    2. mainform.InstanceRef = this;
    3. mainform.Show();
    4. this.Hide();

    Then, in Form1.FormClosed event, I did:

    C# Code:
    1. //Shutdown all timers
    2. tim1.Enabled = false;
    3. tim2.Enabled = false;
    4.  
    5. //Close forms
    6. this.InstanceRef.Dispose();
    7. 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)
    Last edited by pjrage; Apr 25th, 2007 at 01:26 PM.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Sep 2004
    Posts
    185

    Re: Splash screen

    Anyone? Is that the "right way?" Or is there a better way?

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: Splash screen

    I'm pretty sure you would need to use multi-threading. One thread loads your application while the other thread displays the splash screen.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width