pecker
pecker

Reputation: 2037

How to set a TextBox for password input in WinForms?

How to set a TextBox for password input in WinForms? Also I want to show "Capslock is ON" popup if capslock is on.

I want something like

<input type="password" /> in HTML.

Upvotes: 71

Views: 133521

Answers (9)

Vizel Leonid
Vizel Leonid

Reputation: 543

it's easy, look:

  1. Create TextBox control
  2. Click once on it in the form constructor (Open its properties)
  3. Find the category called Behavior
  4. Here you can find 2 properties: PasswordChar and UseSystemPasswordChar

Here you have 2 ways:

  1. Set the UseSystemPasswordChar to True. Symbols in your textbox will probably set to * or another system password char (depending you what system you use)
  2. Set you own char with PasswordChar. For example you can put char like that: ●

Upvotes: 1

Cwenga Zozo
Cwenga Zozo

Reputation: 71

private void cbShowHide_CheckedChanged(object sender, EventArgs e)
{
    if (cbShowHide.Checked)
    {
        txtPin.UseSystemPasswordChar = PasswordPropertyTextAttribute.No.Password;
    }
    else
    {
        //Hides Textbox password
        txtPin.UseSystemPasswordChar = PasswordPropertyTextAttribute.Yes.Password;
    }
}

Copy this code to show and hide your textbox using a checkbox

Upvotes: 5

you can use like these "txtpassword.PasswordChar = '•';"

the use location is ...

 namespace Library_Management_System
    {
        public partial class Login : Form
        {
            public Login()
            {
                InitializeComponent();
                txtpassword.PasswordChar = '•';

Upvotes: 2

NomanJaved
NomanJaved

Reputation: 1380

Just set the property of textbox that is PasswordChar and set the * as a property of textbox. That will work for password.

  passwordtextbox.PasswordChar = '*';

where passwordtextbox is the text box name.

Upvotes: 6

Ahmedshaqanbi
Ahmedshaqanbi

Reputation: 11

I know the perfect answer:

  1. double click on The password TextBox.
  2. write your textbox name like textbox2.
  3. write PasswordChar = '*';.
  4. I prefer going to windows character map and find a perfect hide like ●.

    example:TextBox2.PasswordChar = '●';
    

Upvotes: 1

AxelEckenberger
AxelEckenberger

Reputation: 16926

The best way to solve your problem is to set the UseSystemPasswordChar property to true. Then, the Caps-lock message is shown when the user enters the field and the Caps-Lock is on (at least for Vista and Windows 7).

Another alternative is to set the PasswordChar property to a character value (* for example). This also triggers the automatic Caps-Lock handling.

Upvotes: 77

Charlie
Charlie

Reputation: 15227

To make PasswordChar use the ● character instead:

passwordTextBox.PasswordChar = '\u25CF';

Upvotes: 21

z-boss
z-boss

Reputation: 17608

To set a text box for password input:

textBox1.PasswordChar = '*';

you can also change this property in design time by editing properties of the text box.

To show if "Capslock is ON":

using System;  
using System.Windows.Forms;  
//...
if (Control.IsKeyLocked(Keys.CapsLock)) {  
    MessageBox.Show("The Caps Lock key is ON.");  
}  

Upvotes: 28

Reed Copsey
Reed Copsey

Reputation: 564363

Just set the TextBox.PasswordChar property to '*'.

Upvotes: 14

Related Questions