Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Closing a form (such as a splash screen) from another form 1

Status
Not open for further replies.

SalemGrafix

IS-IT--Management
Jun 12, 2003
46
0
0
US
I have created a splash screen, and once the main form is loaded, I want to close (or dispose of) the splash screen.

Everytime I have tried this, one of two things happens. Either the splash screen stays resident (memory leak issues) or the whole application closes.

The names of the forms are as follows:

frmSplash - Splash Form.
frmMain - The main form that needs to close the splash screen once it's loaded.

In VB6 (the old language I'm used to), this was simple to do, in the load event of the new form, simply unload the splash screen. I'm very new to C#, and so far enjoy it, just sometimes trying to figure out the correct syntax is very strange (and the help files aren't very helpful when most of them don't have examples).

Thanks for any help on this.
 
Hi SalemGrafix, there are a few different ways to do this but I cannot say which suits you without seeing the method that opens the form and where it is opened. That being said here is a few thoughts. If the main form opens up the splash screen could this be opened with :
Code:
frmSplash fs = new frmSplash();
this.Hide();
fs.ShowDialog();//splash can so some things needed for program
this.Show();
now when splash screen is closed the main form will resume.

Another way (that I am not keen on)is to have a class that stores public static variables i.e:
Code:
public class ApplicationForms
{
	//all static forms
	public static Splash fsplash;
}

//in the main class
//this way u carry a lot of baggage through the program
ApplicationForms.fsplash = new Splash();
ApplicationForms.fsplash.ShowDialog();
As you have come from a VB background you may close your app with this.Close();
But I find I have less problems with:
this.Dispose();
Hope this helps a bit.


Age is a consequence of experience
 
Keep in mind that when you say .Show or .ShowDialog the executing code waits until that dialog is closed before it continues. If you want a true splash screen that shows up while you are loading a form in the background, you will want to .ShowDialog in a separate thread until your form has finished loading - and then close it.
 
I'm assuming that what you're asking, is in "Program.cs" how is the first form (Splash screen) opened:

Code:
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            ms_frmSplash = new frmSplash();
            Application.Run(ms_frmSplash);
        }

As for what JurkMonkey was talking about, that is exactly what I'm looking for, is a way to open the splash screen while the program is fully loading, and be disposed of once the main program is displayed.

In VB6, you could easily close the splash form from the main form once it's loaded, by issuing "Unload frmSplash".

Hope that explains what I'm trying to do, and thanks for the help provided so far, and I think the way I want to do it is using the seperate thread for the main form, but I'm not sure how to accomplish this.

Thanks again.
 
Okay, I found another thread on another forum, where someone was saying that if it was the form that contained the main program information, if you closed that, it would cause the whole program to close out. Not quite sure what they are talking about there either (and it seems to be an archived forum, where I can't ask questions on that).

From the way they describe it, it appears that I'm trying to close a valid form, so I'm still baffled as to why when I do close it (from itself, but not from another form), which should occur after the second form has been opened, it shuts down the whole program.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top