Check if TextBox is empty and return MessageBox?

后端 未结 8 1361
谎友^
谎友^ 2020-12-23 22:19

I made this statement to check if TextBox is empty, but the MessageBox always shows up wether the TextBox is empty or not.

    private void NextButton_Click         


        
8条回答
  •  不知归路
    2020-12-23 22:54

    For multiple text boxes - add them into a list and show all errors into 1 messagebox.

    // Append errors into 1 Message Box      
    
     List errors = new List();   
    
     if (string.IsNullOrEmpty(textBox1.Text))
        {
            errors.Add("User");
        }
    
        if (string.IsNullOrEmpty(textBox2.Text))
        {
            errors.Add("Document Ref Code");
        }
    
        if (errors.Count > 0)
        {
            errors.Insert(0, "The following fields are empty:");
            string message = string.Join(Environment.NewLine, errors);
            MessageBox.Show(message, "errors", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            return;
        } 
    

提交回复
热议问题