0

I have created a login form where by default it shows text 'enter username' in user name text field when one clicks on password text field and vice versa shows 'enter password' when clicked on username text field

Now I want to use pwd.passchar = "*" here. But password field doesn't shows 'enter password' as text but *****. It should display the text unless user starts typing actual password

Here is my code -

public Login()
    {
        InitializeComponent();

        if (pwd.Text == "enter password")
        {             
            // Don't show $$ in password text field
        }
        else
        {
            pwd.PasswordChar = '*';
        }
    }



private void textBox1_Click(object sender, EventArgs e)
    {
        uname.Text = string.Empty;

        if (string.IsNullOrWhiteSpace(pwd.Text))
        {
            pwd.Text = "enter password";
        }

    }

private void textBox2_Click(object sender, EventArgs e)
    {
        pwd.Text = string.Empty;

        if (string.IsNullOrWhiteSpace(uname.Text))
        {
            uname.Text = "enter username";

Please suggest changes...

james andy
  • 59
  • 1
  • 10
  • What you really want is a "watermark" or "placeholder" text that disappears when the user starts typing. Trying to build this logic around every single textbox is going to be ugly and unmaintainable. You could follow [this answer](http://stackoverflow.com/a/836463/74757) for a way to decorate a text box, *any* textbox, with this functionality, with very little markup. – Cᴏʀʏ Jul 08 '15 at 21:27

2 Answers2

0

Play with empty string for PasswordChar and TextChanged event

in vb.net

Private Sub pwd_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pwd.TextChanged
    If pwd.Text = "enter password" Then
        pwd.PasswordChar = ""
    Else
        pwd.PasswordChar = "*"
    End If
End Sub

in c#, probe this

    private void pwd_TextChanged(object sender, EventArgs e)
    {
        if (pwd.Text == "enter password")
            pwd.PasswordChar = Convert.ToChar(0);
        else if (pwd.Text == "")
            pwd.Text = "enter password";
        else
           pwd.PasswordChar = '*'; 
    }
Pablo Fébolo
  • 106
  • 1
  • 4
  • I get error for (I think) the double quotes but then when I use single quotes ie pwd.PasswordChar = ' ', i get error - Empty character literal.. – james andy Jul 09 '15 at 15:08
  • Since I got my desired result playing with your code. I am going to upvote you answer. – james andy Jul 10 '15 at 17:43
0

This worked for me -

if (pwd.Text == "enter password" || pwd.Text == "")
        {
            //Do nothing. Enter Password text will be seen
        }
        else
        {
            pwd.PasswordChar = '*';
        }
james andy
  • 59
  • 1
  • 10