Splash Screen Example - c#

I only want a simple Splash Screen Example.
Get the Code, Insert my picture, add 2 lines of code to load and finish.
But all I can google is so complex, that is too much. I only want a form with a picture that goes more and more transparent until it hides automaticly and my window is shown.
I tried the "prettygoodsplashscreen" from Codeproject, but don't work for me.
Lang is c#.net 2.0

Creating the splash screen can be as simple or complex as you make/want it to be.
private void Form1_Load(object sender, System.EventArgs e)
{
// Display the splash screen
var splashScreen = new SplashForm();
splashScreen.Show()
// On the splash screen now go and show loading messages
splashScreen.lblStatus.Text = "Loading Clients...";
splashScreen.lblStatus.Refresh();
// Do the specific loading here for the status set above
var clientList = _repository.LoadClients();
// Continue doing this above until you're done
// Close the splash screen
splashScreen.Close()
}
Obviously the Splash Screen itself is something that you'd have to decide how you want it to look...

In order for your splash screen to be areal splash screen, it shouldn't have other code than displaying about what it's doing (loading clients, for instance) or show the progress of application startup through a ProgressBar control.
Here are the steps:
Instantiate a BackgroundWorker for which you will launch the loading within the BackgroundWorker.DoWork() method;
Within your main Form_Load() event, call BackgroundWorker.RunWorkerAsync() method;
Still in your Form_Load() event, after your call to RunWorkerAsync(), instantiate and display your splash screen to your user SplashForm.ShowDialog()
Report progress from within your LoadClient() method, for instance, with the BackgroundWorker.ProgressChanged() event (you may also report what it is your BackgroundWorker is doing ("loading clients...");
In your RunWorkerCompleted() event, you may Splash.Close() your splash screen form.
I shall add some further details later on. Have to go now.

Related

How to close all open and hidden forms in C# with one button?

I have a Splash Screen to my program.
And when the splash end , exec the code
this.Hide();
frmLogin o = new frmLogin();
o.show();
And it works but the splash screen go invisible and when i close the program by my custom exit button it's only closing the current form.
But my splash screen is still hidden and appears the app name to task manager.
How i can close that currently opened form and the invisible ones with my custom button ?
You are launching your whole application from the splash screen form.
It would be better avoid this behaviour separating splash screen form from main form, and opening up from this last form the splash screen, as well as you have done with frmLogin.
However you can workaround this problem using this.Hide() and at the exit of the program using Application.Exit() within your "custom exit button" event.
While in design view, select the frmLogin button. On the right, the Properties window should update. Select the Events tab. Look for FormClosing in the list of events. Select the right column and type a method name of your choice. If you typed xBtnPress, the designer will generate a stub that will look like this:
private void xBtnPress(object sender, FormClosingEventArgs e) {
}
If you wish to close your application upon this event, call Application.Exit() from within the event handler.

Splash screen window does not load items

I'm trying to use a window as splash screen. I have this:
{
InitializeComponent();
new splash().ShowDialog();
}
in my main window to start up with and it works but on the window that I'm using as splash when I press start it stays blank. This is the code I'm using for the splash window:
public partial class splash : Window
{
public splash()
{
InitializeComponent();
}
private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
System.Threading.Thread.Sleep(3000);
Close();
}
As it is it just loads the window blank and after 3secs it moves on to the main window.
I want the splash window to load with a label and an image.
Any ideas?
Thanks
So, as I already mentioned it in my comment, here is an excellent guide on how to implement a splash screen for WPF applications. Also Microsoft offers an easier way if your splash screen is only an image (see here).
But the main problem with your code is the Sleep(3000) call, as it blocks the UI thread. Use a Timer instead, which you can start in the window loaded event handler, and close the window in the Timer's Elapsed event handler.
Hope this helps.
(Sorry for almost duplicating my comment, but at the third edit I realized it actually should be an answer :))

Display a form from the main form on startup

I want to display a small form when the program is ran. This is the code:
private void Form1_Load(object sender, EventArgs e)
{
this.Opacity = 0;
InitForm init = new InitForm();
init.Show();
init.BringToFront();
comm = init.Start();
this.Opacity = 100;
}
The Start() method draws some lines to the list box on the InitForm.
I have two problems:
When the program starts, the list box is not populated, I just get
the waiting cursor, then the main form is displayed and the box is
populated at the same time;
The InitForm is behind the main form. How do I bring it to front?
Can't see a ListBox in that code. But painting doesn't happen until the UI thread goes idle again, after your Load event handler completes. Which also means that the Opacity assignment doesn't accomplish anything.
The Z-order problem is (partly) caused by this too, the main form isn't visible yet so BringToFront() doesn't work. Use either Show(this) so that InitForm is an owned window that always displays in front on the main form (recommended) or use the Shown event instead.
When the program starts, the list box is not populated, I just get the
waiting cursor, then the main form is displayed and the box is populated
at the same time;
What do you expect should happen? Your UI Thread is busy in executing the code below
this.Opacity = 0;
InitForm init = new InitForm();
init.Show();
init.BringToFront();
comm = init.Start();
this.Opacity = 100;
Once it gets freed it shows both the forms and list box populated. It is behaving correctly in my opinion
this.Opacity = 0;
Above line won't have any effect because UI thread will execute all the lines first then it will display the UI which means by the time UI shows something this.Opacity = 100; would have already been executed
I want to display a small form when the program is ran. The InitForm
is behind the main form. How do I bring it to front?
Why don't you set the Small Form as startup form and load the MainForm in load method of small form?
You should load the secondary form on a paralell thread.
So, on the Form1's load event handler you trigger the second thread, showing the Form2.
You can bring the Init form to front by setting this property
init.TopMost=true
It worked for me, check out,

WPF UI - New form to be displayed while progress in original form

I have make a file upload/download functionality where the front end is a WPF UI.
I have figured out that to make my UI not freeze, I need to do the upload/download in a separate thread.
But I also need to show a progress bar while uploading/downloading. I want to do this by showing a new WPF form with a progress bar, and during the upload/download the original form should be made inactive for the user ( user may not click any button etc. ); user can only see the progress bar moving in the new form; upon completion, the new form needs to close and original form becomes active again.
Can someone please help me.
Thanks.
You can implement a simple progress bar. Take this as a start: http://www.codeproject.com/KB/WPF/WpfProgressBar.aspx You can update the progress bar via a callback method from the downloader thread to update the progress every second or so.
First of all, to show a modal Window, all you need to do is
myModalWindow.ShowDialog();
If you use the Show() method, it will just show the window. But if you use the ShowDialog() method, all other windows in your WPF app will not respond to user input until that window closes.
Secondly, you can update a progress bar from another thread by using the UI thread's dispatcher.
Application.Current.Dispatcher.BeginInvoke(() => myProgressBar.Value = progress);

Hide WPF Window Until Fully Loaded

For my WPF application, I am storing several user settings like window position, window state, and whether or not to display a welcome dialog. The problem is that while everything is loading up, I see a lot of flashing and flickering as the windows are loaded in, and then more flickering when the window is maximized after reading in the settings.
I am already using the built-in WPF PNG splash screen functionality, but is there a way to completely hide the rendering of all windows until everything is fully loaded in?
Edit the Application.xaml, remove the StartUpUri, instead set the StartUp event handler.
In Application.xaml.cs, edit the startup event handler to display the splashscreen, load your resources, create everything, then create the main window and show it.
<Application
...
StartUp="OnStartUp"
/>
And:
private void OnStartUp(Object sender, StartupEventArgs e)
{
var settings = LoadSettingsFrom... // Call your implementation of load user settings
// Example only, in real app do this if's section on a different thread
if (settings.doShowSplashScreen)
{
var splashScreen = new SplashScreen();
splashScreen.Show();
}
// Load and create stuff (resources, databases, main classes, ...)
var mainWindow = new mainWindow();
mainWindow.ApplySettings(settings); // Call your implementation of apply settings
if (doShowSplashScreen)
{
// send close signal to splash screen's thread
}
mainWindow.Show(); // Show the main window
}
You can set the windows WindowState to Minimized, then handle the ContentRendered event and set the WindowState to Normal or Maximized.
There are functions , BeginInit and EndInit, if you change properties inside these functions like..
BeginInit();
...
... // Do your code Initialization here...
...
EndInit();
then your window will not render until the EndInit() is called, it will not flicker.
When does this loading occur? Code executed in the main Window's constructor should execute before the window is shown; if you load any required resources there, you should not see any flickering.

Categories