Viewing a splashscreen before viewing the form of an application  
Author Message
Ricardo Redert





PostPosted: Visual C# Express Edition, Viewing a splashscreen before viewing the form of an application Top

I'm working on an application, a webbrowser. I followed the documentation in the book "Build a program now! Visual C# 2005 express edition", Chapter 6.

The problem is that the splashscreen is viewed, but in the background you can see a part of the form, that should be visible afted the splashscreen dissapeares.

I created a splashscreen.

In the constructor of the form of my browser I wrote the following code:

Splash splashScreen = new Splash();

public Browser()

{

InitializeComponent();

splashScreen.Show();

Application.DoEvents();

}

When the form is finished loading it throws an "Activated" event. I catch this event with the following method:

private void Browser_Activated(object sender, EventArgs e)

{

Thread.Sleep(3000);

splashScreen.Close();

}

So the idea here is that the splashscreen is visible from the start (not the form). In the meanwhile the form is loading. When it's done loading it throws an Activated event, wich I catch in the Browser_Activated method. In this method I wait for 3 seconds, then close the splashscreen. After that the form should be visible. But the problem is that the form is directly from the start of the application half visible. You can see the borders en the three buttons in the right corner of the form (the close, minimalize and maximalize) buttons.

What am I doing wrong



Visual Studio Express Editions13  
 
 
Appel





PostPosted: Visual C# Express Edition, Viewing a splashscreen before viewing the form of an application Top

If you don't want the main form to be visible until after something has been done then don't show it until you want to.

Maybe if you described in more detail what it is you want to accomplish someone could help you.


 
 
Ricardo Redert





PostPosted: Visual C# Express Edition, Viewing a splashscreen before viewing the form of an application Top

I want the splashscreen to be visible. In the meanwhile the main form is loading. But the loading is finished in less then a second, so I built in a delay of three seconds. After that, the splashscreen must dissapear and the mainform must be visible.

In my first message I explained how this shoul work according to the book I'm using, but it doesn't work properly.

So I have to questions. What am I doing wrong, en how can I fix it


 
 
Appel





PostPosted: Visual C# Express Edition, Viewing a splashscreen before viewing the form of an application Top

What do you mean by half visible


 
 
Ricardo Redert





PostPosted: Visual C# Express Edition, Viewing a splashscreen before viewing the form of an application Top

I tried to make a screenshot, but it didn't work. You see the borders of the form, the header with the windows buttons (minimize, maximize and close) and a white area where al the content within the form is.

You get the same result if you are debugging with a breakpoint. I hope this makes it clear to you.


 
 
Appel





PostPosted: Visual C# Express Edition, Viewing a splashscreen before viewing the form of an application Top

Sorry, didn't notice before. You are using Thread.Sleep, so during those three seconds your application does nothing, and that includes drawing. What you probably want to do is add a timer and start the timer in your activate event handler and in the tick event handler for the timer close the splash screen.
 
 
Ricardo Redert





PostPosted: Visual C# Express Edition, Viewing a splashscreen before viewing the form of an application Top

Thank you for your answers so far.

Yess, I understand that when I put a thread to sleep, the drawing stops. According to the documentation in the book the form shouldn't be visible until the thread is active after the 'sleeping'.

To solve my question I ask you another question. How do I make my mainform unvisible until I want it to be visible. If I know the answer to that question I hide the mainform until the 'sleeping' of the thread is over. After that I close my splashscreen and I make my mainform visible.


 
 
Appel





PostPosted: Visual C# Express Edition, Viewing a splashscreen before viewing the form of an application Top

Not sure if this will work the way you want it. There are plenty of ways to handle splash screens depending on your requirements.

Change the Main method to so that it runs the Application.Run method that takes no arguments. Then change your splash class so that it closes itself after some time using a timer. In your main class listen for the splash forms FormClosed event and call the main class Show method in the event handler.

Program.cs



static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
new Browser();
Application.Run();
}
}

Browser.cs



public partial class Browser : Form
{
public Browser()
{
InitializeComponent();
Splash splash = new Splash();
splash.FormClosed += new FormClosedEventHandler(form2_FormClosed);
splash.Show(this);
}
void form2_FormClosed(object sender, FormClosedEventArgs e)
{
Show();
}
}

Splash.cs



public partial class Splash : Form
{
public Splash()
{
InitializeComponent();
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
Close();
}
}