none
How to catch carriage return and only CR in textbox input in C# program? RRS feed

  • Question

  • Yes, this is my question.

    There are two password related properties in textBox control: PasswordChar and UseSystemPasswordChar. The latter seems to be clear in meaning but how about the former? Can it be used to set up the password?

    Thanks, - MyCatAlex


    • Edited by MyCatAlex Saturday, November 16, 2019 11:41 PM
    Saturday, November 16, 2019 10:57 PM

Answers

  • Hi MyCatAlex, 

    Thank you for posting here.

    ‘TextBox.UseSystemPasswordChar’ property indicates whether the text in the TextBox control should appear as the default password character.

    If you set it true, when you enter words in textbox, you will get the following result:

    ‘TextBox.PasswordChar’ property can make you set custom character used to mask characters of a password.

    If you set it ‘*’.

    You will get the following result.

    Hope it can help you.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by MyCatAlex Monday, November 18, 2019 10:54 PM
    Monday, November 18, 2019 5:32 AM
    Moderator

All replies

  • In the following example there is a TextBox named PasswordTextBox and a CheckBox named ShowHidePasswordCheckBox.

    The following toggles using the password char in CheckChanged event of the CheckBbox.

    private void ShowHidePasswordCheckBox_CheckedChanged(object sender, EventArgs e)
    {
       PasswordTextBox.PasswordChar = ShowHidePasswordCheckBox.Checked ? '*' : '\0';
    }

    Working with detecting enter

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            var tb = new TextBox();
            tb.KeyDown += tb_KeyDown;
    
        }
        private void tb_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                //enter key is down
            }
        }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by MyCatAlex Sunday, November 17, 2019 4:03 PM
    • Unmarked as answer by MyCatAlex Monday, November 18, 2019 1:41 AM
    Sunday, November 17, 2019 12:04 AM
    Moderator
  • The title of the question is about catching the CR in a textbox, but then you ask about the PasswordChar. Both things are completely unrelated.

    The PasswordChar refers to the character that will be displayed on screen to hide the "real" characters of the password. Typically you would set it to an asterisk or similar character. When the user types the password, the textbox then presents asterisks instead of the characters typed (which cannot include a CR, the textbox does not allow it unless you set it to "Multiline" -- but this has nothing to do with the password char).

    • Marked as answer by MyCatAlex Sunday, November 17, 2019 4:02 PM
    • Unmarked as answer by MyCatAlex Monday, November 18, 2019 1:41 AM
    Sunday, November 17, 2019 11:55 AM
    Moderator
  • Thank you Alberto and especially Karen. Karen, you've helped me before and Alberto as well a big helper, helped big time. - MyCatAlex
    Sunday, November 17, 2019 4:05 PM
  • Karen hi,

    Although I praised you, you gave me totally defective code that does not work. e.KeyCode is highlighted as an error.

    And what is this? "

    //enter key is down"

    - MyCatAlex

    P.S. Checkbox does not work either. It replaces characters entered with asterisks and that's it. BAD CODE.



    • Edited by MyCatAlex Sunday, November 17, 2019 8:42 PM
    Sunday, November 17, 2019 8:00 PM
  • Hi MyCatAlex, 

    Thank you for posting here.

    ‘TextBox.UseSystemPasswordChar’ property indicates whether the text in the TextBox control should appear as the default password character.

    If you set it true, when you enter words in textbox, you will get the following result:

    ‘TextBox.PasswordChar’ property can make you set custom character used to mask characters of a password.

    If you set it ‘*’.

    You will get the following result.

    Hope it can help you.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by MyCatAlex Monday, November 18, 2019 10:54 PM
    Monday, November 18, 2019 5:32 AM
    Moderator
  • Karen hi,

    Although I praised you, you gave me totally defective code that does not work. e.KeyCode is highlighted as an error.

    And what is this? "

    //enter key is down"

    - MyCatAlex

    P.S. Checkbox does not work either. It replaces characters entered with asterisks and that's it. BAD CODE.



    It's not bad code, if you took a minute to add one line after creating the TextBox then it works.  And in regards to // enter key is down.

    It could be

    private void tb_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            MessageBox.Show("Enter key detected");
        }
    }

    Or perhaps (and note the missing line to add the TextBox to the form's control collection)

    using System.Windows.Forms;
    
    namespace WindowsFormsApp1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                var tb = new TextBox();
                Controls.Add(tb);
                tb.KeyDown += tb_KeyDown;
            }
            private void tb_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Enter)
                {
                    var work = new Work();
                    work.Execute();
                }
            }
        }
    
        public class Work
        {
            public void Execute()
            {
                MessageBox.Show("Got here from pressing ENTER in TextBox");
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, November 19, 2019 1:03 AM
    Moderator