Showing posts with label WPF. Show all posts
Showing posts with label WPF. Show all posts

Saturday 21 August 2010

How to Add a Splash Screen to a WPF Application

Adding a Splash Screen to a WPF Application is really easy. For this I will be using VS 2010.

First prepare your image using an image editing application (Such as Photoshop). For this example I will be using the following image that I created using the MSPaint application.

Splash

 

Create a WPF application using VS 2010. And add your Splash Screen image to the project.

Add Existing

 

And change the build action of your image to “SplashScreen”.

Build Action

And when you run the application you can see the Splash Screen with a fading effect, before the Main Window.

But there are times that we want to keep the splash screen for more than the default time (300 Milliseconds). In order to do that, follow these steps.

Change the application’s build action from “Application Definition” to “Page”

Build Action 2

 

Create a constructor, and call the “InitializeComponent” method within that.

public App() {
InitializeComponent();
}







Write your own Main method. And call for the splash screen within that main method. And you can state the amount of time that you wish to keep your splash screen visible.



[STAThread]
public static void Main() {
SplashScreen sc = new SplashScreen("Splash.png");
sc.Show(true);
SplashScreenExample.App app = new SplashScreenExample.App();
sc.Close(TimeSpan.FromMilliseconds(4000));
app.Run(new MainWindow());
}


Now go to project properties and change the startup object from “Not Set” to your app class name (In this example its “SplashScreenExample.App”).


Startup Object 


Now when you run your application, you can see that the splash screen stays for 4 seconds. (Including the fading time)

Monday 9 August 2010

Create a Multiline TextBox Control using WPF [WPF – Multiline Text Box]

In windows applications we set the “Multiline” property to “true” in order to make a textbox multiline. But in WPF, there’s no property called “Multiline”. In order to make textbox multiline in WPF, you have to set three properties. That is :

  • TextWrapping=”Wrap”
  • VerticalScrollBarVisibility=”True”
  • AcceptsReturn=”True”

 

E.g

<TextBox Name="txtaddress" 
TextWrapping="Wrap"
VerticalScrollBarVisibility="Visible"
AcceptsReturn="True"/>