dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: text box and password

  1. #1
    Join Date
    Apr 2008
    Posts
    21

    Angry text box and password

    I have a text box which I wish to use for setting a password. When the user presses a key I wish the character ‘*’ to be displayed, also I do not wish for more than five characters to be entered and no two characters to be the same. I wish to store the characters entered in a String called pswrd. How might I do this? I know I have to make use of PasswordChar and MaxLength but I do not know how to code it. Do I code it inside an event like Key_Press?

  2. #2
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: text box and password

    i suggest that you read some book before coming here and post;
    without reading book you will be have a lot of problem and if you want ask all of them soon you will be tired.

    as i see most of your questions will be answered in the frist 200-300 pages of a good book.
    also you can search google and MSDN for such simple questions.

    all of us maybe ask sometimes questions that are simple or obvious and we have not cared to them but always asking simple question shows that the one who ask need to read more and study.

    it was a kindly advice.
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

  3. #3
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: text box and password

    Password: in designer, select the textbox, go to propertieswindow and typ '*' at PasswordChar, at runtime the textbox will automaticcaly show this char.

    Length: in designer, select the textbox, go to propertieswindow and typ '5' at MaxLength, at runtime, everthing that is inserted in the textbox that will exceed this length is ignored.


    Further, checking if a character is unique, you need to do some program work.

    Code:
    string pswrd = textBox1.Text;
    foreach(string char in pswrd){
       //do something with your char, save it somewhere and check if it is unique
    }
    Last edited by dannystommen; December 15th, 2008 at 10:44 AM.

  4. #4

    Re: text box and password

    Here is the method for this process as well, in detail:

    http://msdn.microsoft.com/en-us/libr...swordchar.aspx

    Example:

    Code:
    public void CreateMyPasswordTextBox()
     {
        // Create an instance of the TextBox control.
        TextBox textBox1 = new TextBox();
        // Set the maximum length of text in the control to eight.
        textBox1.MaxLength = 8;
        // Assign the asterisk to be the password character.
        textBox1.PasswordChar = '*';
        // Change all text entered to be lowercase.
        textBox1.CharacterCasing = CharacterCasing.Lower;
        // Align the text in the center of the TextBox control.
        textBox1.TextAlign = HorizontalAlignment.Center;
     }
    Last edited by ZOverLord; December 15th, 2008 at 10:49 AM.

  5. #5
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: text box and password

    Quote Originally Posted by ZOverLord View Post
    Here is the method for this process as well, in detail:

    http://msdn.microsoft.com/en-us/libr...swordchar.aspx

    Example:

    Code:
    public void CreateMyPasswordTextBox()
     {
        // Create an instance of the TextBox control.
        TextBox textBox1 = new TextBox();
        // Set the maximum length of text in the control to eight.
        textBox1.MaxLength = 8;
        // Assign the asterisk to be the password character.
        textBox1.PasswordChar = '*';
        // Change all text entered to be lowercase.
        textBox1.CharacterCasing = CharacterCasing.Lower;
        // Align the text in the center of the TextBox control.
        textBox1.TextAlign = HorizontalAlignment.Center;
     }
    why set the textbox's CharacterCasing to lower? A strong password contains both lower and uppercase characters (and also numbers)

  6. #6

    Re: text box and password

    The example code is from Microsoft, click the link in the post, where the code is displayed.

  7. #7
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Re: text box and password

    Quote Originally Posted by raydona View Post
    Do I code it inside an event like Key_Press?
    Code:
            private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                e.Handled = ((TextBox)sender).Text.IndexOf(e.KeyChar) != -1;            
            }
    however you may still need to check duplicates in validating event
    Code:
            private void textBox1_Validating(object sender, CancelEventArgs e)
            {            
                string s = ((TextBox)sender).Text;
                for (int i = 1; i < s.Length - 1; i++)
                    if (s.IndexOf(s[i], i + 1) != -1)
                    {
                        e.Cancel = true;
                        break;
                    }
    
            }
    you can also do the checking on events like TextChange, Keydown, etc..

    hth
    Busy

  8. #8
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: text box and password

    I have problem with your question:

    1- In General it is recommanded that paswwords be at least 6 Characters but you need at most 5 characters!

    2- passwords usually are saved on a storage like database or encrypted files and there is no need you check that user has entered identical characters because if any combination that does not exist and the database will fail you may need such validation when the user want to register and enter a password whereas in that situation you don't need *. user should see what password he is registering and then you can validate it base on the standard rules.

    Note: if it is a practice for you; it would be better that you do some practice that are simalar to the situations in the real world and application. first have a good analyze for the problem then try to start coding.
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

  9. #9
    Join Date
    May 2007
    Posts
    1,546

    Re: text box and password

    It looks like a standard tutorial question for college, so I wouldn't be too concerned about security
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  10. #10
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: text box and password

    no, it is not standard tutorial question; standard one should apply to the real world
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

  11. #11
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,116

    Re: text box and password

    Quote Originally Posted by toraj58 View Post
    no, it is not standard tutorial question; standard one should apply to the real world
    That makes absolutely NO sense. The original poster gave the requirements for the password (max 5 characters, no duplicates). This is a given FACT. Any discussion as to the security of this type of password, is off-topic, and should NOT be brought into the thread.

    ps: This EXACT set of requirements shows up regularly in December, and in April/May for multiple years. This indicates that it is part of a school study program of some type....
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  12. #12
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: text box and password

    my problem is exactly with such school study programs; is it hard to give studies to students that apply to the real world?
    about the security issue please look at it as a add-on to this thread that is very not far away from his question and password issue. BTW i have seen programmers that have accoustemd to wrong things just becuase their teacher have told them.
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

  13. #13
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,116

    Re: text box and password

    Quote Originally Posted by toraj58 View Post
    my problem is exactly with such school study programs; is it hard to give studies to students that apply to the real world?
    about the security issue please look at it as a add-on to this thread that is very not far away from his question and password issue. BTW i have seen programmers that have accoustemd to wrong things just becuase their teacher have told them.
    That is not the point. If the assignment (assuming it is an assignment) has the given requirements, and a student turns in an ultra-secure implementation (8 or more, at least one from each of the 4 groups, repeat usage prevention, DES or better encryption), the student should FAIL the assignment.

    Going off on tangents can cause more confusion as the person may not be able to use them for one or more reasons. If you want to discuss password security issues, then it would be appropriate to create a NEW thread on that topic so that such discussions do not get in the way of the purpose of the current thread.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  14. #14
    Join Date
    May 2007
    Posts
    1,546

    Re: text box and password

    Quote Originally Posted by toraj58 View Post
    my problem is exactly with such school study programs; is it hard to give studies to students that apply to the real world?
    They need to learn to program before they can even start learning about best practices or security. Walk before you run!
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  15. #15
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: text box and password

    Quote Originally Posted by THECpuWizard
    the student should FAIL the assignment
    this is rational.
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)