Home > Software engineering >  textbox text is checked only once
textbox text is checked only once

Time:06-08

I have 2 panels. If textboxes are empty, there is a message. My problem is, say I get to second panel then go back and delete texts then go to second panel again, it does not show message. Is there a way to check the texts every time?

    private void bttn_Next_Click(object sender, EventArgs e)
    {
        if (txt_FirstName.Text == " " || txt_LastName.Text == " " ||
            txt_Email.Text == " " || txt_Contact.Text == " " ||
            txt_HouseNumber.Text == " " || txt_Street.Text == " " || 
            txt_Barangay.Text == " " || txt_Municipality.Text == "")
        {
            MessageBox.Show("Please complete all required fields.", "Message");
        }
        else
        {
            panel1.Hide();
            panel2.Hide();
            panel3.Show();
        }
    }

CodePudding user response:

The correct way to check for empty string is as follows

    private void bttn_Next_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrWhiteSpace(txt_FirstName.Text) || string.IsNullOrWhiteSpace(txt_LastName.Text) ||
            string.IsNullOrWhiteSpace(txt_Email.Text) || string.IsNullOrWhiteSpace(txt_Contact.Text) ||
            string.IsNullOrWhiteSpace(txt_HouseNumber.Text) || string.IsNullOrWhiteSpace(txt_Street.Text) ||
            string.IsNullOrWhiteSpace(txt_Barangay.Text) || string.IsNullOrWhiteSpace(txt_Municipality.Text))
        {
            MessageBox.Show("Please complete all required fields.", "Message");
        }
        else
        {
            panel1.Hide();
            panel2.Hide();
            panel3.Show();
        }
    }
  • Related