ALI
ALI

Reputation: 339

Validate empty TextBox, NumericUpdown and ComboBox with generic code in C#

I have about 10 Forms like this below ScreenShot. enter image description here

I have to reset them and Validate if they are empty before inserting the data in to database.

Reset code :

void ClearAllText(Control con)
{
    foreach (Control field in con.Controls)
    {
        if (field is TextBox)
            ((TextBox)field).Clear();
        else if (field is ComboBox)
            ((ComboBox)field).Text = "Select ";
        if (field is DataGridView)
            ((DataGridView)field).Rows.Clear();//.Clear();
        else if (field is RichTextBox)
            ((RichTextBox)field).Clear();
        else if (field is NumericUpDown)
            ((NumericUpDown)field).Value = 0;
        else
            ClearAllText(field);
    }
}

private void action_Reset_Click(object sender, EventArgs e)
{
    ClearAllText(this);
}

Above code is working pretty much fine. But when I try to validate then it's not working properly.

private void action_Insert_Click(object sender, EventArgs e)
{
    foreach (Control c in panel6.Controls)
    {
        if (c is TextBox)
        {
            if (c.Text.Equals(""))
            {
                MessageBox.Show("Some Values Are Empty or Not Proper... ", "Error Message",
            MessageBoxButtons.OK, MessageBoxIcon.Error);
                break;
            }
        }
    }
}

So Far I have tried only TextBox and I have to validate NumericUpDown and Comobox too.

Upvotes: 2

Views: 437

Answers (1)

phduarte
phduarte

Reputation: 380

Since the controls do not have a default Reset or Empty Validation interface, you will have to manually implement the logic. At least refactoring the code you can reuse it in more places.

private void action_Insert_Click(object sender, EventArgs e)
{
    //validation
    if (Controls.OfType<Control>().Any(x => !IsEmpty(x)))
    {
        MessageBox.Show("Some Values Are Empty or Not Proper... ", "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
        return;
    }

    //insert statement
}

private void action_Reset_Click(object sender, EventArgs e)
{
    ClearAllText(this);
}

void ClearAllText(Control con)
{
    foreach (Control c in con.Controls) Clear(c);
}

private bool IsEmpty(Control control)
{
    if (control is TextBox txt)
        return txt.Text == string.Empty;

    if (control is ComboBox cmb)
        return cmb.Text == "Select ";

    if (control is DataGridView dgv)
        return dgv.DataSource == null;

    if (control is RichTextBox rtb)
        return rtb.Text == string.Empty;

    if (control is NumericUpDown nud)
        return nud.Value == 0;

    return true;
}

private void Clear(Control control)
{
    if (control is TextBox txt)
        txt.Clear();
    else if (control is ComboBox cmb)
        cmb.Text = "Select ";
    else if (control is DataGridView dgv)
        dgv.DataSource = null;
    else if (control is RichTextBox rtb)
        rtb.Clear();
    else if (control is NumericUpDown nud)
        nud.Value = 0;
}

Upvotes: 1

Related Questions