简体   繁体   中英

Message box is not displaying the message

I have written a set of functions to validate the textboxes in my form for their required field needs like this

private void ValidateForm()

        {
        //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;
        }

But it is not displaying the proper messages.

I may be mis-interpreting your IF construct of

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

but I think you want

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

Lets say your answers were all TRUE (ie: valid), then its interpreting it as

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

If one of the first two was FALSE and the last was ok, you would have

IF ( FALSE AND FALSE AND ( TRUE == FALSE))

by doing the logical NOT (!) and checking if any one of them fail is what you want.

if NOT ( all 3 parts valid )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM