Introduction:
In this article I am creating a simple
application for login and registration using WPF in visual studio 2010. In this
application I am creating two window forms one is for Registration and another
is for login. First of all user will register after then he/she can login.
Now I am going to discuss in
brief about this application.
Step1:
Open Visual Studio 2010 -> File -> New -> Project.
New project template will
display and select WPF Application like as follows:

Figure 1:
Step 2:
Enter your project name and click on OK button.
Step 3: You will get the MainWindow.xaml window form, if
you want to change then rename it and also change the StartupUri property of application into App.xaml like
as follows:
App.xaml:
<Application
x:Class="Login_WPF.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Registration.xaml">
<Application.Resources></Application.Resources>
</Application>
Step 4: Now design your Registration page as figure 2 or
copy and page following inline code.
Registration.xaml:
<Window
x:Class="Login_WPF.Registration"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Registration"
Height="387"
Width="528"
Background="Black">
<Grid Height="350"
Width="525"
Background="Bisque">
<TextBlock
Height="23"
HorizontalAlignment="Left"
Margin="10,5,0,0"
Name="textBlockHeading"
Text="Registration:"
VerticalAlignment="Top"
Width="110" FontSize="17"
FontStretch="ExtraCondensed"/>
<!--Button as a Link
button using style-->
<Button
Margin="451,5,12,288"
Content="Login"
Cursor="Hand"
Click="Login_Click">
<Button.Template>
<ControlTemplate
TargetType="Button">
<TextBlock
TextDecorations="Underline">
<ContentPresenter
/>
</TextBlock>
</ControlTemplate>
</Button.Template>
<Button.Style>
<Style
TargetType="Button">
<Setter
Property="Foreground"
Value="Navy"
/>
<Style.Triggers>
<Trigger
Property="IsMouseOver"
Value="true">
<Setter
Property="Foreground"
Value="Red"
/>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
<!--end Button as a
Link button using style-->
<Grid
Margin="31,0,29,23"
Background="White"
Height="264"
VerticalAlignment="Bottom">
<Grid.RowDefinitions>
<RowDefinition
Height="252*"
/>
<!-- <RowDefinition
Height="12*" />-->
</Grid.RowDefinitions>
<TextBlock
Height="20"
HorizontalAlignment="Left"
Margin="67,0,0,0"
x:Name
="errormessage"
VerticalAlignment="Top"
Width="247" OpacityMask="Crimson"
Foreground="#FFE5572C"
/>
<TextBlock
Height="23"
HorizontalAlignment="Left"
Margin="67,20,0,0"
Name="textBlockFirstname"
Text="First
Name:"
VerticalAlignment="Top"
Width="110"
/>
<TextBlock
Height="23"
HorizontalAlignment="Left"
Margin="67,50,0,0"
Name="textBlockLastName"
Text="Last
Name:"
VerticalAlignment="Top"
Width="110"
/>
<TextBlock
Height="23"
HorizontalAlignment="Left"
Margin="67,80,0,0"
Name="textBlockEmailId"
Text="EmailId"
VerticalAlignment="Top"
Width="110"
/>
<TextBlock
Height="23"
HorizontalAlignment="Left"
Margin="67,107,0,0"
Name="textBlockPassword"
Text="Password:"
VerticalAlignment="Top"
Width="110" />
<TextBlock
Height="23"
HorizontalAlignment="Left"
Margin="67,136,0,0"
Name="textBlockConfirmPwd"
Text="ConfirmPassword:"
VerticalAlignment="Top"
Width="110"
Grid.RowSpan="2"
/>
<TextBlock
Height="23"
HorizontalAlignment="Left"
Margin="67,166,0,0"
Name="textBlockAddress"
Text="Address"
VerticalAlignment="Top"
Width="110"
/>
<TextBox
Height="23"
HorizontalAlignment="Left"
Margin="183,20,0,0"
Name="textBoxFirstName"
VerticalAlignment="Top"
Width="222"
/>
<TextBox
Height="23"
HorizontalAlignment="Left"
Margin="183,50,0,0"
Name="textBoxLastName"
VerticalAlignment="Top"
Width="222"
/>
<TextBox
Height="23"
HorizontalAlignment="Left"
Margin="183,80,0,0"
Name="textBoxEmail"
VerticalAlignment="Top"
Width="222"
/>
<PasswordBox
Height="23"
HorizontalAlignment="Left"
Margin="183,107,0,0"
Name="passwordBox1"
VerticalAlignment="Top"
Width="222"
/>
<!--For password-->
<PasswordBox
Height="23"
HorizontalAlignment="Left"
Margin="183,136,0,0"
Name="passwordBoxConfirm"
VerticalAlignment="Top"
Width="222"
/>
<TextBox
Height="23"
HorizontalAlignment="Left"
Margin="183,0,0,66"
Name="textBoxAddress"
VerticalAlignment="Bottom"
Width="222"
/>
<Button
Content="Submit"
Height="23"
HorizontalAlignment="Left"
Margin="183,204,0,0"
Name="Submit"
VerticalAlignment="Top"
Width="70"
Click="Submit_Click"
/>
<Button
Content="Reset"
Height="23"
HorizontalAlignment="Left"
Margin="259,204,0,0"
Name="button2"
VerticalAlignment="Top"
Width="70"
Click="button2_Click"
/>
<Button
Content="Cancel"
Height="23"
HorizontalAlignment="Right"
Margin="0,204,60,0"
Name="button3"
VerticalAlignment="Top"
Width="70"
Click="button3_Click"
/>
</Grid>
</Grid>
</Window>

Figure 2: Registration form
Registration.xaml.cs:
Imports
System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports
System.Windows
Imports
System.Windows.Controls
Imports
System.Windows.Data
Imports
System.Windows.Documents
Imports
System.Windows.Input
Imports
System.Windows.Media
Imports
System.Windows.Media.Imaging
Imports
System.Windows.Shapes
Imports System.Data
Imports
System.Data.SqlClient
Imports
System.Text.RegularExpressions
Namespace
Login_WPF
''' <summary>
''' Interaction logic for Registration.xaml
'''
</summary>
Partial
Public Class Registration
Inherits Window
Public Sub New()
InitializeComponent()
End Sub
Private Sub
Login_Click(ByVal sender
As Object,
ByVal e As
RoutedEventArgs)
Dim login
As New Login()
login.Show()
Close()
End Sub
Private Sub
button2_Click(ByVal sender
As Object,
ByVal e As
RoutedEventArgs)
Reset()
End Sub
Public Sub
Reset()
textBoxFirstName.Text = ""
textBoxLastName.Text = ""
textBoxEmail.Text = ""
textBoxAddress.Text = ""
passwordBox1.Password = ""
passwordBoxConfirm.Password = ""
End Sub
Private
Sub button3_Click(ByVal sender
As Object,
ByVal e As
RoutedEventArgs)
Close()
End Sub
Private Sub
Submit_Click(ByVal sender
As Object,
ByVal e As
RoutedEventArgs)
If textBoxEmail.Text.Length = 0
Then
errormessage.Text = "Enter an
email."
textBoxEmail.Focus()
ElseIf Not
Regex.IsMatch(textBoxEmail.Text, "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$")
Then
errormessage.Text = "Enter a
valid email."
textBoxEmail.[Select](0, textBoxEmail.Text.Length)
textBoxEmail.Focus()
Else
Dim firstname
As String =
textBoxFirstName.Text
Dim lastname
As String =
textBoxLastName.Text
Dim email
As String =
textBoxEmail.Text
Dim password
As String =
passwordBox1.Password
If passwordBox1.Password.Length
= 0 Then
errormessage.Text =
"Enter password."
passwordBox1.Focus()
ElseIf
passwordBoxConfirm.Password.Length = 0 Then
errormessage.Text =
"Enter Confirm password."
passwordBoxConfirm.Focus()
ElseIf passwordBox1.Password <>
passwordBoxConfirm.Password Then
errormessage.Text =
"Confirm password must be same as password."
passwordBoxConfirm.Focus()
Else
errormessage.Text = ""
Dim address
As String =
textBoxAddress.Text
Dim con
As New
SqlConnection("Data Source=TESTPURU;Initial Catalog=Data;User
ID=sa;Password=wintellect")
con.Open()
Dim cmd
As New
SqlCommand("Insert into Registration (FirstName,LastName,Email,Password,Address)
values('" & firstname & "','" &
lastname & "','" & email &
"','" & password &
"','" & address &
"')", con)
cmd.CommandType = CommandType.Text
cmd.ExecuteNonQuery()
con.Close()
errormessage.Text = "You have
Registered successfully."
Reset()
End If
End
If
End Sub
End Class
End Namespace
Now I am taking another form
for login as follows:
Login.xaml:
<Window
x:Class="Login_WPF.Login"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Login"
Height="350"
Width="525">
<Grid>
<TextBlock
Height="23"
HorizontalAlignment="Left"
Margin="10,10,0,0"
Name="LoginHeading"
Text="Login:"
VerticalAlignment="Top"
FontSize="17"
FontStretch="ExtraCondensed"/>
<TextBlock
Height="50"
HorizontalAlignment="Left"
Margin="24,48,0,0"
Name="textBlockHeading"
VerticalAlignment="Top"
FontSize="12"
FontStyle="Italic"
Padding="5">
Note: Please login here to view the
features of this site. If you are new on this site then <LineBreak
/><!--line
break-->
please click on
<!--textblock as a
Hyperlink.-->
<TextBlock>
<Hyperlink
Click="buttonRegister_Click"
FontSize="14"
FontStyle="Normal">Register</Hyperlink>
</TextBlock>
<!--end textblock as
a hyperlink-->
button
</TextBlock>
<TextBlock
Height="23"
HorizontalAlignment="Left"
Margin="66,120,0,0"
Name="textBlock1"
Text="Email"
VerticalAlignment="Top"
Width="67"
/>
<TextBlock
Height="23"
HorizontalAlignment="Left"
Margin="58,168,0,0"
Name="textBlock2"
Text="Password"
VerticalAlignment="Top"
Width="77"
/>
<TextBox
Height="23"
HorizontalAlignment="Left"
Margin="118,115,0,0"
Name="textBoxEmail"
VerticalAlignment="Top"
Width="247"
/>
<PasswordBox
Height="23"
HorizontalAlignment="Left"
Margin="118,168,0,0"
Name="passwordBox1"
VerticalAlignment="Top"
Width="247"
/>
<Button
Content="Login"
Height="23"
HorizontalAlignment="Left"
Margin="118,211,0,0"
Name="button1"
VerticalAlignment="Top"
Width="104"
Click="button1_Click"
/>
<TextBlock
Height="23"
HorizontalAlignment="Left"
x:Name
="errormessage"
VerticalAlignment="Top"
Width="247"
Margin="118,253,0,0" OpacityMask="Crimson"
Foreground="#FFE5572C" />
</Grid>
</Window>

Figure 3: Login form.
Login.xaml.cs:
Imports
System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports
System.Windows
Imports
System.Windows.Controls
Imports
System.Windows.Data
Imports
System.Windows.Documents
Imports
System.Windows.Input
Imports
System.Windows.Media
Imports
System.Windows.Media.Imaging
Imports
System.Windows.Navigation
Imports
System.Windows.Shapes
Imports System.Data
Imports
System.Data.SqlClient
Imports
System.Text.RegularExpressions
Namespace
Login_WPF
''' <summary>
''' Interaction logic for MainWindow.xaml
'''
</summary>
Partial Public Class Login
Inherits Window
Public Sub New()
InitializeComponent()
End Sub
Private registration
As New
Registration()
Private welcome
As New Welcome()
Private Sub
button1_Click(ByVal sender
As Object,
ByVal e As
RoutedEventArgs)
If textBoxEmail.Text.Length = 0
Then
errormessage.Text = "Enter an
email."
textBoxEmail.Focus()
ElseIf Not
Regex.IsMatch(textBoxEmail.Text, "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a
zA-Z\.]*[a-zA-Z]$") Then
errormessage.Text = "Enter a
valid email."
textBoxEmail.[Select](0, textBoxEmail.Text.Length)
textBoxEmail.Focus()
Else
Dim email
As String =
textBoxEmail.Text
Dim password
As String =
passwordBox1.Password
Dim con
As New SqlConnection("Data
Source=TESTPURU;Initial Catalog=Data;User ID=sa;Password=wintellect")
con.Open()
Dim cmd
As New SqlCommand("Select
* from Registration where Email='" & email &
"' and password='" & password &
"'", con)
cmd.CommandType = CommandType.Text
Dim adapter
As New
SqlDataAdapter()
adapter.SelectCommand = cmd
Dim dataSet
As New DataSet()
adapter.Fill(dataSet)
If dataSet.Tables(0).Rows.Count
> 0 Then
Dim username
As String =
dataSet.Tables(0).Rows(0)("FirstName").ToString()
& " " & dataSet.Tables(0).Rows(0
("LastName").ToString()
welcome.TextBlockName.Text = username
'Sending value from one form to
another form.
welcome.Show()
Close()
Else
errormessage.Text =
"Sorry! Please enter existing emailid/password."
End If
con.Close()
End If
End Sub
Private Sub
buttonRegister_Click(ByVal sender
As Object,
ByVal e As
RoutedEventArgs)
registration.Show()
Close()
End Sub
End Class
End Namespace
Welcome.xaml:
<Window
x:Class="Login_WPF.Welcome"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Welcome"
Height="250"
Width="400">
<Grid>
<TextBlock
Height="23"
HorizontalAlignment="Left"
Margin="10,10,0,0"
x:Name="WelcomeHeading"
Text="Welcome:"
VerticalAlignment="Top"
FontSize="17"
FontStretch="ExtraCondensed"/>
<TextBlock
Height="23"
HorizontalAlignment="Left"
Margin="90,10,0,0"
x:Name="TextBlockName" VerticalAlignment="Top"
FontSize="15"
FontStretch="ExtraCondensed"
/>
</Grid>
</Window>
At last if user login
successfully then welcome message will display on another form as shown below:

Figure 4: