I'm creating a C# windows based program in which I need to make sure that when a user types his/her password it will show it masked with stars like this: *******.
-
2Winforms? WPF? A Console app? – Liath Jul 18 '14 at 10:39
-
The initial text is usually referred to as a watermark. A search for that word + text box + whatever technology you're using should find solutions. – Damien_The_Unbeliever Jul 18 '14 at 10:44
-
1We would need to know the framework you are using in order to answer correctly, but basically you want to use a Password Box and set the hint property. – Nahuel Ianni Jul 18 '14 at 10:48
2 Answers
Try the code after creating a textbox named textbox1 in front end
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
public class Form2
{
private void Form2_Load(System.Object sender, System.EventArgs e)
{
TextBox1.Text = "Enter your security Password";
}
public void change(TextBox txt_pwd)
{
txt_pwd.Text = "";
txt_pwd.PasswordChar = "*";
}
private void TextBox1_Click(object sender, System.EventArgs e)
{
change(TextBox1);
}
private void TextBox1_GotFocus(object sender, System.EventArgs e)
{
change(TextBox1);
}
public Form2()
{
Load += Form2_Load;
}
}

- 1,326
- 1
- 12
- 27
Every framework that contains a UI element has a control called PasswordBox or similar.
Basically, you should be using that one.
This control contains a property that allows you to select which special character to show in order to represent a character inside the box, for example the PaswwordChar.
Finally, the ToolTip property will allow you to set a hint on it as asked on the title of your question.
So for example:
PasswordBox pw = new PasswordBox();
pw.MaxLength = 25;
pw.PasswordChar = '*';
pw.ToolTip = "I'm a password!"
Will create a password box that replaces every character in it with a start symbol (*) and show a small, yellow pop up when the mouse pointer hovers over it showing the "I'm a password!" message.
Also, the control will not allow passwords of over 25 characters long.
The above links are for WPF, but the logic will not vary whether you use winforms, silverlight or some other UI framework.
EDIT:
If what you are looking for is a watermark property for your passwordbox, then you should check the existing answers like WPF Watermark PasswordBox from Watermark TextBox

- 1
- 1

- 3,177
- 4
- 23
- 30
-
He wanted the text to be *in* the password textbox though, which is usually called either a watermark or a hinttext or nulltext or similar. – Lasse V. Karlsen Jul 18 '14 at 11:06