locked
Password box for entering a fixed length password RRS feed

  • Question

  • Hi,

    The password that will be entered in the password box will be of fixed length. As soon as the last character of the password is entered a new window should open up without clicking on a button. How should I proceed about?

    Sunday, June 19, 2016 4:59 PM

Answers

  • You could compare the Length of the Text with the expected length of the password as suggested by Brian Muth and then open a window by creating an instance of it and call its Show method:

    <PasswordBox x:Name="pb" PasswordChanged="pb_PasswordChanged" MaxLength="10" />

            private void pb_PasswordChanged(object sender, RoutedEventArgs e)
            {
                if (pb.Password.Length >= pb.MaxLength)
                {
                    Window1 win = new Window1();
                    win.Show();
                }
            }

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.


    Sunday, June 19, 2016 5:15 PM
  • The Text property of the TextBox returns the entire string that is in the TextBox. The event handler will be invoked for each keystroke and the if condition will only be fullfilled as you type in the last character:

    private void textBox3_TextChanged(object sender, EventArgs e)
            {
                TextBox txtBox = sender as TextBox;
                string fullPassword = txtBox.Text;
    
                if(fullPassword.Length == 10)
                {
                    //...open form..
                }
            }
    

    But please ask Windows Forms related questions in the appropriate forum: https://social.msdn.microsoft.com/Forums/windows/en-US/home?forum=winforms

    If you are developing a WPF application, there is a WPF forum as well: https://social.msdn.microsoft.com/Forums/en-US/home?forum=wpf

    This forum is intended for general questions about the C# programming language.

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread in an appropriate forum if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by DotNet Wang Monday, June 27, 2016 9:34 AM
    Thursday, June 23, 2016 8:58 PM

All replies

  • This sounds straightforward. Just intercept the TextChanged event, and examine the length of the text.

    Something like:

            private void textBox1_TextChanged (object sender, EventArgs e)
            {
                if (textBox1.Text.Length >= MaxPasswordLength)
                {
                    // do stuff here
                }
            }

    • Proposed as answer by Magnus (MM8)MVP Sunday, June 19, 2016 5:11 PM
    • Marked as answer by Titiksha Daga Wednesday, June 22, 2016 10:04 AM
    • Unmarked as answer by Titiksha Daga Wednesday, June 22, 2016 10:04 AM
    Sunday, June 19, 2016 5:05 PM
  • You could compare the Length of the Text with the expected length of the password as suggested by Brian Muth and then open a window by creating an instance of it and call its Show method:

    <PasswordBox x:Name="pb" PasswordChanged="pb_PasswordChanged" MaxLength="10" />

            private void pb_PasswordChanged(object sender, RoutedEventArgs e)
            {
                if (pb.Password.Length >= pb.MaxLength)
                {
                    Window1 win = new Window1();
                    win.Show();
                }
            }

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.


    Sunday, June 19, 2016 5:15 PM
  • Thank you!!!!
    Sunday, June 19, 2016 5:16 PM
  • When i use the text changed event handler the code takes only 1 character and not the whole string
    Wednesday, June 22, 2016 10:06 AM
  • The Text property of the TextBox returns the entire string that is in the TextBox. The event handler will be invoked for each keystroke and the if condition will only be fullfilled as you type in the last character:

    private void textBox3_TextChanged(object sender, EventArgs e)
            {
                TextBox txtBox = sender as TextBox;
                string fullPassword = txtBox.Text;
    
                if(fullPassword.Length == 10)
                {
                    //...open form..
                }
            }
    

    But please ask Windows Forms related questions in the appropriate forum: https://social.msdn.microsoft.com/Forums/windows/en-US/home?forum=winforms

    If you are developing a WPF application, there is a WPF forum as well: https://social.msdn.microsoft.com/Forums/en-US/home?forum=wpf

    This forum is intended for general questions about the C# programming language.

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread in an appropriate forum if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by DotNet Wang Monday, June 27, 2016 9:34 AM
    Thursday, June 23, 2016 8:58 PM