2010-11-25 6 views
1

J'ai écrit un ensemble de fonctions pour valider les zones de texte dans ma forme pour leurs besoins sur le terrain requis comme celui-ciboîte de message n'affiche le message

private void valideFormulaire()

 { 
     //Initialise the variables for validation check and call the related functions 
     bool bisValidhost = ValidateHost(); 
     bool bisValidPassword = ValidatePassword(); 
     bool bisUsername = ValidateUsername(); 

     //If any of the entries is missing then show error message 
     if(bisValidhost && bisValidPassword && bisUsername == false) 
      { 
      MessageBox.Show("This is not a Valid Entry!");     
      }   
     } 


    /// <summary> 
    /// This function validate the Required field need of txtHost. 
    /// </summary> 
    /// <returns></returns> 
    private bool ValidateHost() 
     { 
     ErrorProvider errorProvider = new ErrorProvider(); 
     bool isValid = true; 

     //If the txtHost is empty, show a message to user 
     if(txtHost.Text == string.Empty) 
      { 
      errorProvider.SetError(txtHost, "Please enter the host address"); 
      isValid = false; 
      } 
     else 
      errorProvider.SetError(txtHost, string.Empty); 
     return isValid; 
     } 


    ///<summary> 
    /// This function validate the Required field need of txtUsername. 
    /// </summary> 
    /// <returns></returns> 
    /// </summary> 
    /// <returns></returns> 
    private bool ValidateUsername() 
     { 
     ErrorProvider errorProvider = new ErrorProvider(); 
     bool isValid = true; 

     //If the txtUsername is empty, show a message to user 
     if(txtUsername.Text == string.Empty) 
      { 
      errorProvider.SetError(txtUsername, "Please enter the Username"); 
      isValid = false; 
      } 
     else 
      errorProvider.SetError(txtUsername, string.Empty); 
     return isValid; 
     } 


    ///<summary> 
    /// This function validate the Required field need of txtPassword. 
    /// </summary> 
    /// <returns></returns> 
    /// </summary> 
    /// <returns></returns> 
    private bool ValidatePassword() 
     { 
     ErrorProvider errorProvider = new ErrorProvider(); 
     bool isValid = true; 

     //If the txtPassword is empty, show a message to user 
     if(txtPassword.Text == string.Empty) 
      { 
      errorProvider.SetError(txtPassword, "Please enter the Password"); 
      isValid = false; 
      } 
     else 
      errorProvider.SetError(txtPassword, string.Empty); 
     return isValid; 
     } 

Mais il est n'affiche pas les messages appropriés.

+0

Ceci est une application Windows ou une application Web –

Répondre

6

je être mal interpréter vos SI construire des

if(bisValidhost && bisValidPassword && bisUsername == false) 

mais je pense que vous voulez

if(! (bisValidhost && bisValidPassword && bisUsername)) 

Disons que vos réponses étaient TRUE (ie: valide), puis son interpréter comme

if (TRUE and TRUE and (TRUE == FALSE)) 

Si l'un des deux premiers était faux et le dernier était ok, vous auriezEn faisant le NOT (!) Logique et en vérifiant si l'un d'entre eux échoue, c'est ce que vous voulez.

si NON (toutes les 3 parties valides)

Questions connexes