Pages

Tuesday 12 March 2013

Password Control in WPF

Password Control in WPF Window Based

The password box control is a special type of TextBox designed to enter passwords. The typed in characters are replaced by asterisks. Since the password box contains a sensible password it does not allow cut, copy, undo and redo commands.

The PasswordBox control allows you to hide the characters and limit the number of characters to be typed in the editable area. 
 
Properties:

Password property contains the password in the PasswordBox and PasswordChar is the masking character for the password.

Following tag create the Password Box control with the masking character as * and maximum password length of 50 characters.

The MaxLength property is used to get and set the maximum number of characters you can enter in a PasswordBox.
 
Event: To handle the event add the PasswordChanged="Password1_OnPasswordChanged" 
PasswordChanged event is main event of the control and is raised when password property 
has been changed in the xaml and write the handler as

 

protected void Password1_OnPasswordChanged(object sender, RoutedEventArgs e)
{
     Console.WriteLine(PasswordBox1.Password);
}

 
Example
 
 
XAML Code- 

<Window x:Class="WpfApplication7.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="370" Width="417">
    <Grid Background="AliceBlue">
        <PasswordBox Height="23" Margin="85,91,0,0" Name="passwordBox1"
 VerticalAlignment="Top" HorizontalAlignment="Left" Width="102" Password="gupta"/>
        <TextBox Height="23" Margin="85,56,113,0" Name="textBox1" 
 VerticalAlignment="Top" HorizontalAlignment="Left" Width="102  " />
        <Label Height="23" HorizontalAlignment="Left" Margin="17,56,0,0" Name="label1" 
VerticalAlignment="Top" Width="81">Username</Label>
        <Label Height="23" HorizontalAlignment="Left" Margin="17,91,0,0" Name="label2" 
VerticalAlignment="Top" Width="81">Password</Label>
        <Button Margin="85,130,0,0" Name="button1" Click="button1_click" HorizontalAlignment="Left" 
Width="85" Height="29" VerticalAlignment="Top">Login</Button>
    </Grid>
</Window>

No comments:

Post a Comment