• 0

Simple Password enter in a WPF C# app


Question

trying to create a simple password entry into a Window(WPF) which is called MainSite but can't seem to get the code working


<Window x:Class="Password_protection.Password"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Password" Height="350" Width="525">
<Grid>
<TextBox Height="28" HorizontalAlignment="Left" Margin="230,102,0,0" Name="passwordBox1" VerticalAlignment="Top" Width="130" TextChanged="OnPropertyChanged" HorizontalContentAlignment="Left" VerticalContentAlignment="Center" />
<Label Content="Enter Password" Height="28" HorizontalAlignment="Left" Margin="130,102,0,0" Name="label1" VerticalAlignment="Top" Width="94" />
<Button Content="Enter Site" Height="23" HorizontalAlignment="Left" Margin="416,276,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
<Button Content="Cancel" Height="23" HorizontalAlignment="Left" Margin="325,276,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click" />
</Grid>
</Window>
[/CODE]

[CODE]


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;


namespace Password_protection
{
/// <summary>
/// Interaction logic for Password.xaml
/// </summary>
public partial class Password : Window
{
private String password = "Password";
public Password()
{
InitializeComponent();
}

private void OnPropertyChanged(object sender, TextChangedEventArgs e)
{


}

private void Cancel(object sender, RoutedEventArgs e)
{
Close();
}

private void Enter(object sender, RoutedEventArgs e)
{
if (password.Length == 0)
{
MessageBoxResult result = MessageBox.Show("NothingEntered" + Environment.NewLine + "Enter password.", "End program", MessageBoxButton.YesNo);
if (result == MessageBoxResult.Yes)
{
this.Hide();
var wdwPassword = new Password();
wdwPassword.ShowDialog();
this.Show();
}
else
{
this.Close();
}
}

else if (passwordBox1 != password)
{
MessageBoxResult result = MessageBox.Show("Errornvironment.NewLine + "Entered password must be same as set password", "End program", MessageBoxButton.YesNo);
if (result == MessageBoxResult.Yes)
{
this.Hide();
var wdwPassword = new Password();
wdwPassword.ShowDialog();
this.Show();
}
else
{
this.Close();
}
}

else
{
this.Hide();
var wdwMainSite = new MainSite();
wdwMainSite.ShowDialog();
this.Show();
this.Close();
}
}
}
}
[/CODE]

6 answers to this question

Recommended Posts

  • 0
  On 02/11/2012 at 17:37, Riva said:

Your buttons point to button1_Click and button2_Click events but your methods are called Enter and Cancel


<Window x:Class="Password_protection.Password"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Password" Height="350" Width="525">
<Grid>
<TextBox Height="28" HorizontalAlignment="Left" Margin="230,102,0,0" Name="passwordBox1" VerticalAlignment="Top" Width="130" TextChanged="OnPropertyChanged" HorizontalContentAlignment="Left" VerticalContentAlignment="Center" />
<Label Content="Enter Password" Height="28" HorizontalAlignment="Left" Margin="130,102,0,0" Name="label1" VerticalAlignment="Top" Width="94" />
<Button Content="Enter Site" Height="23" HorizontalAlignment="Left" Margin="416,276,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="Enter" />
<Button Content="Cancel" Height="23" HorizontalAlignment="Left" Margin="325,276,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="Cancel" />
</Grid>
</Window>
[/CODE]

don't know why it says button1_Click but the program complies with this error (Error 1 Operator '!=' cannot be applied to operands of type 'System.Windows.Controls.TextBox' and 'string')

but i think my logic might be wrong when the enduser enters the right static password which is Password then it should allow access to the MainSite.xaml if nothing is entered then a messageBox displays nothing is entered, if the wrong password is enter,then again message to try again. but as i click the enter site button it just goes to the MainSite.xaml. also none of the messageBoxs are working.

  • 0
  On 02/11/2012 at 18:25, SasukeOfTheLeaf said:

don't know why it says button1_Click but the program complies with this error (Error 1 Operator '!=' cannot be applied to operands of type 'System.Windows.Controls.TextBox' and 'string')

If there are any compilation errors then the code does not compile. If you ask Visual Studio to run the "last successful build" it will run just that, the last successful build - not the build of your current code, because your current code doesn't build.

This compilation error probably points to

passwordBox1 != password[/CODE]

which is illegal because passwordBox1 is a TextBox and password is a String; they cannot be compared directly. You want to compare its Text property, i.e.

[CODE]passwordBox1.Text != password[/CODE]

.

  • 0

c# how do you change the inputted text in the textBox to ***** to hide the password entered i've tried


passwordBox1.PasswordChar = '*';
[/CODE]

but PasswordChar throws an Error 1 'System.Windows.Controls.TextBox' does not contain a definition for 'PasswordChar' and no extension method 'PasswordChar' accepting a first argument of type 'System.Windows.Contrs.TextBox' could be found (are you missing a using directive or an assembly reference?)

i've tried these three lines of code

[CODE]
((TextBox)this.passwordBox1.TextBoxElement.TextBoxItem.HostedControl).UseSystemPasswordChar = true;
passwordBox1.UseSystemPasswordChar = true;
passwordBox1.PasswordChar = '*';
[/CODE]

none seem to do the job

  • 0
  On 02/11/2012 at 23:24, Riva said:

Are you a VB coder? :D

No offense intended.

I would rather do:

string passGiven = passwordBox1.Text;

if(!passGiven.Equals(password))

{

...

Also, to test if the pass is empty

if(string.IsNullOrWhiteSpace(passGiven))

{

...

started off using VB and switched to C# because VB is well just crap :p

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.