I’m working on a kind of Screen manager, here’s the code:
/// <summary>
/// Change the screen according to type
/// </summary>
/// <author>Kay van Bree</author>
/// <param name="type">Replacement screen type</param>
public void ChangeScreen(ScreenType type)
{
// Swap old screen with loading screen
ReplaceScreen(screen, new LoadingScreen(this));
UserControl newScreen;
// Get instance of correct screen
switch(type)
{
case ScreenType.login:
newScreen = new LoginScreen(this);
break;
case ScreenType.dashboard:
newScreen = new DashboardScreen(this);
break;
default:
newScreen = new LoginScreen(this);
break;
}
// Swap loading screen with new screen
ReplaceScreen(screen, newScreen);
Text = "Attendance Tracker | " + screen;
}
private void ReplaceScreen(UserControl oldScreen, UserControl newScreen)
{
Controls.Remove(oldScreen);
screen = newScreen;
// Initialize screen
newScreen.BackColor = Color.Transparent;
newScreen.Location = new Point((Size.Width - screen.Size.Width) / 2, (Size.Height - screen.Size.Height) / 2);
Controls.Add(newScreen);
}
The goal of this function is to show a Loading Screen while it’s initializing another UserControl (the screens are subclasses of UserControl). When the UserControl is initialized it removes the Loading screen and adds the UserControl to controls.
The problem is that my Loading screen (also a UserControl) won’t show up. It won’t execute any code before the other UserControl has been initialized.
I can’t seem to find a solution, or what the problem is at all. Can you explain this behaviour? Am I approaching the loading screen wrong? Does C# load the constructor in a different thread or something? What’s the problem?
[edit] By the way. The loading screen is just something I wanted to add in. The rest of the screen manager works just fine, so if I’m approaching it wrong, I’ll probably drop the whole loading thing.
A simple Refresh() solved my problem.