Assuming you have a Form with two TextBoxes on it, textBox1, and textBox2: you want the two TextBoxes to display initial messages in plain-text, like the first one to display: "Enter Password," and the second one to display: "Re-Enter Password."
When the user causes either one of the TextBoxes to have 'Focus, and begins typing, you wish the initial plain-text message to disappear, and then whatever the user types will appear as your "masked" password-character, like: "*"
When the user hits the "Enter" key in either TextBox, you want to compare their contents: if they match, you are done; if they don't match, you want to reset the two TextBoxes to their initial state.
Note: there are two ways for either of the TextBoxes to get 'Focus: one is by the user clicking the mouse inside one of them (MouseDown), and the other is by the user hitting the TabKey to make them the active control that has 'Focus.
Now, let's look at some code:
private const char EnterChar = (char) Keys.Enter;
private const char EmptyChar = '\0';
private const char MaskChar = '*';
private string text1 = "Enter Password";
private string text2 = "Re-Enter Password";
private bool tb1Virgin = true;
private bool tb2Virgin = true;
private void Form1Load(object sender, EventArgs e)
{
textBox1.Text = text1;
textBox2.Text = text2;
textBox1.PasswordChar = EmptyChar;
textBox2.PasswordChar = EmptyChar;
}
private void TextBox1MouseDown(object sender, MouseEventArgs e)
{
if (textBox1.PasswordChar == EmptyChar)
{
textBox1.Clear();
textBox1.PasswordChar = MaskChar;
tb1Virgin = false;
}
}
private void TextBox2MouseDown(object sender, MouseEventArgs e)
{
if (textBox2.PasswordChar == EmptyChar)
{
textBox2.Clear();
textBox2.PasswordChar = MaskChar;
tb2Virgin = false;
}
}
private void PasswordTextBoxesReset()
{
MessageBox.Show("No Match: try again");
textBox1.PasswordChar = EmptyChar;
textBox1.Text = text1;
tb1Virgin = true;
textBox2.PasswordChar = EmptyChar;
textBox2.Text = text2;
tb2Virgin = true;
}
private void TextBox1KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == EnterChar)
{
if (textBox1.Text != textBox2.Text)
{
PasswordTextBoxesReset();
}
else
{
MessageBox.Show("Password Match = " + textBox1.Text);
}
}
else
{
if (tb1Virgin)
{
textBox1.Clear();
textBox1.PasswordChar = MaskChar;
tb1Virgin = false;
}
}
}
private void TextBox2KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == EnterChar)
{
if (textBox1.Text != textBox2.Text)
{
PasswordTextBoxesReset();
}
else
{
MessageBox.Show("Password Match = " + textBox1.Text);
}
}
else
{
if (tb2Virgin)
{
textBox2.Clear();
textBox2.PasswordChar = MaskChar;
tb2Virgin = false;
}
}
}
Notes:
0. Note that setting the PasswordChar property of a TextBox to '\0' has the effect of making everything typed in the TextBox plain-text.
1. The two TextBoxes have the 'MultiLine property set to 'false. Remember that the value of the 'AcceptsReturn property of the TextBoxes doesn't matter here, because they are not 'MultiLine TextBoxes. And, note, that "Enter" and "Return" are the same in .NET.
2. You could certainly re-factor this code, and make both textBox1 and textBox2 share the same 'MouseDown, and 'KeyPress, EventHandlers, but, for educational purposes here, each TextBox has its own EventHandlers. And, for something this simple: why bother to re-factor ?
3. Obviously, when there is a "match," you'll probably want to hide the TextBoxes, or switch to some other Form, or whatever.
4. It would be easy, of course, to "decorate" the two TextBoxes with either Labels, or use a Tool-Tip, to give the user information like the minimum number of characters to enter, whether they must enter at least one number, etc. Other nice touches are: if a match fails, give the user some appropriate feedback based on analysis of what they entered if it doesn't meet your validation criteria, like: "your password must contain at least one number," etc.