C# WPF passwordbox , changing 'password' property

Hi Guys,


I trying to create a basic user log-in and so far, if they are a first time user, can create a Username and Password, the info of which goes into a List<> called 'userlist' which stores there given Password and Username.

Now I would ideally like to use a masked textbox for the password field.....which in WPF turns out to be called a 'Password' box.

The problem is I can't figure out how to connect the password box 'password' (content) to the password given in the userlist.

It seems to me you can only have 1 PWbox with a pre-difined password....or am I just missing a trick here?

my current code is

        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            foreach (User u in userlist)
            {

                if ( u.userName != tbUserbox.Text)
                {
                    if ( tbPasswordbox.Equals(u.passWord))
                    {
                        tabPW.Visibility = Visibility.Visible;
                        tabCnpw.Visibility = Visibility.Visible;
                            tabUS.Visibility = Visibility.Visible;

                            tabLogin.Visibility = Visibility.Collapsed;

                    }

                }
            }
        }

Open in new window


Many Thanks for the help.

John
Bronco78thAsked:
Who is Participating?
 
tpaynCommented:
Couple of things here:

1.) your code is checking if the username is not equal to the list user it should be "u.userName == tbUserbox.Text"

2.) Use the Password Property to get the current password in the Password box i.e. "tbPasswordbox.Password == u.passWord"

3.) you need to break out of the foreach loop when you have found your user
private void btnLogin_Click(object sender, RoutedEventArgs e)
 {
      foreach (User u in userlist)
      {
         if ( u.userName == tbUserbox.Text)
         {
             if ( tbPasswordbox.Password == u.passWord)
             {
                 tabPW.Visibility = Visibility.Visible;
                 tabCnpw.Visibility = Visibility.Visible;
                 tabUS.Visibility = Visibility.Visible;
                 tabLogin.Visibility = Visibility.Collapsed;
              }
              
              break;
         }
     }
}

Open in new window

0
 
Bronco78thAuthor Commented:
Works great, one thing im slightly unclear on.

When do I add a break and when I do not to a loop?

Many Thanks.

John
0
 
tpaynCommented:
Use the break statement when you need to exit a loop early.

In this instance you did not need to process any of the other users in your list cause you have found the correct user.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.