2017-12-27 9 views
0

현재 Visual Studio에서 sign-up 양식을 디자인하고 있는데 문제가 있습니다. 첫 번째 사진에서 알 수 있듯이 기본 텍스트가있는 두 개의 텍스트 상자가 있습니다. 그 후, 나는 그들을 위해 enter 및 leave 이벤트 처리기를 만들었습니다.
내 목표입니다. 사용자가 텍스트 상자를 클릭하면 기본 텍스트가 사라지고 사용자는 별표로 대체 될 암호를 입력하기 시작할 수 있습니다. 사용자가 값을 입력하지 않고 텍스트 상자를 떠날 때 기본 텍스트가 다시 표시됩니다. 그러나 지금 내 문제은 기본 텍스트가 별표로 변환된다는 것입니다. 이는 내가 원하지 않는 것입니다. 여기 enter image description here
enter image description here 당신은 텍스트를 마스킹에서 텍스트 상자를 중지하기 위해 PasswordChar를 재설정해야 내 코드사용자가 텍스트 상자를 나갈 때 텍스트 상자의 기본 텍스트로 다시 변환하는 방법은 무엇입니까?

private void txtPassword_Enter(object sender, EventArgs e) 
    { 
     if (txtPassword.Text == "Enter a password...") 
     { 
      txtPassword.Text = ""; 
      txtPassword.PasswordChar = '*'; // Mask characters of a password to asterisk. 
      txtPassword.ForeColor = Color.Black; 
     } 
    } 

    private void txtPassword_Leave(object sender, EventArgs e) 
    { 
     if(txtPassword.Text == "") 
     { 
      txtPassword.Text = "Enter a password..."; 
      txtPassword.ForeColor = Color.Gray; 
     } 
    } 

답변

3

의 샘플입니다.
귀하의 Leave 이벤트 핸들러가 다음과 같이 나타납니다 :

private void txtPassword_Leave(object sender, EventArgs e) 
{ 
    if(txtPassword.Text == "") 
    { 
     txtPassword.PasswordChar = '\0'; // Note this line! 
     txtPassword.Text = "Enter a password..."; 
     txtPassword.ForeColor = Color.Gray; 
    } 
} 
+0

와우 작동! 정말 고맙습니다!! Btw, \ 0 의미는 무엇입니까? – XxS0ul678

+0

'\ 0'은 (는) "null 문자"입니다. –