消息框未显示消息

时间:2010-11-25 13:19:07

标签: c# visual-studio winforms

我编写了一组函数来验证表单中的文本框,以满足他们所需的字段需求

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

但它没有显示正确的信息。

1 个答案:

答案 0 :(得分:6)

我可能会错误地解释你的IF构造

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

但我认为你想要

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

让我们说你的答案都是正确的(即:有效),然后将其解释为

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

如果前两个中的一个是假的而且最后一个是好的,那么你将有

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

通过执行逻辑NOT(!)并检查它们中的任何一个是否失败是你想要的。

如果不是(所有3个部分都有效)