Check if TextBox is empty and return MessageBox?

后端 未结 8 1268
谎友^
谎友^ 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:44

    Well, you are clearing the textbox right before you check if it's empty

    /* !! This clears the textbox BEFORE you check if it's empty */
    MaterialTextBox.Clear();
    
    HoursNumericUpDown.Value = HoursNumericUpDown.Minimum;
    MarkNumericUpDown.Value = MarkNumericUpDown.Minimum;
    
    if (String.IsNullOrEmpty(MaterialTextBox.Text))
    {
            MessageBox.Show("Enter Material Name Please.", "Error", MessageBoxButtons.OK,    MessageBoxIcon.Warning);
                //dataGridView1.Rows.Clear();
    }
    
    0 讨论(0)
  • 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<string> errors = new List<string>();   
    
     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;
        } 
    
    0 讨论(0)
提交回复
热议问题