简体   繁体   中英

How to manage Label in c#

I am trying to organize Label and TextBoxes in c# i have two function :

private void BtnKaydet_Click(object sender, EventArgs e)
{
    _service = new Client.BioAuthenticationService.BioAuthenticationService();
    int warningcase = 0;

    if ((string.IsNullOrEmpty(TbTcNo.Text) || string.IsNullOrWhiteSpace(TbTcNo.Text)))
    {
        warningcase = 1;
        TextLabelManagement(warningcase);                              
    }
    else if ((string.IsNullOrEmpty(TbId.Text) || string.IsNullOrWhiteSpace(TbId.Text)))
    {
        warningcase = 2;
        TextLabelManagement(warningcase);               
    }
    else if ((string.IsNullOrEmpty(TbName.Text) || string.IsNullOrWhiteSpace(TbName.Text)))
    {
        warningcase = 3;
        TextLabelManagement(warningcase);      
    }
    else if ((string.IsNullOrEmpty(TbSurname.Text) || string.IsNullOrWhiteSpace(TbSurname.Text)))
    {
        warningcase = 4;
        TextLabelManagement(warningcase); 
    }
    else if ((string.IsNullOrEmpty(TbDepartment.Text) || string.IsNullOrWhiteSpace(TbDepartment.Text)))
    {
        warningcase = 5;
        TextLabelManagement(warningcase);
    }

    else
    {
        if (_imageIndex == 3)
        {
            bool enrollResult = _service.CheckAndEnrollUser(image1, image2, image3, 300, 6);
        }
        else
        {
            warningcase = 6;
            TextLabelManagement(warningcase);
        }
    }
}

Here i write cases if TextBox is null i will give error message to fill them. Here is my cases :

private void TextLabelManagement(int cases)
{
    switch (cases)
    {
        case 1:
            LblWarning.Visible = true;
            LblWarning.Text = "* Lütfen Bu Alanları Doldurunuz..";
            LblTcNo.Text = "* TC No";
            LblTcNo.ForeColor = System.Drawing.Color.Red;
            LblWarning.ForeColor = System.Drawing.Color.Red; 
            break;
        case 2:
            LblWarning.Visible = true;
            LblWarning.Text = "* Lütfen Bu Alanları Doldurunuz..";
            LblId.Text = "* ID";
            LblId.ForeColor = System.Drawing.Color.Red;
            LblWarning.ForeColor = System.Drawing.Color.Red;
            break;
        case 3:
            LblWarning.Visible = true;
            LblWarning.Text = "* Lütfen Bu Alanları Doldurunuz..";
            LblName.Text = "* Ad";
            LblName.ForeColor = System.Drawing.Color.Red;
            LblWarning.ForeColor = System.Drawing.Color.Red;
            break;
        case 4:
            LblWarning.Visible = true;
            LblWarning.Text = "* Lütfen Bu Alanları Doldurunuz..";
            LblSurname.Text = "* Soyad";
            LblSurname.ForeColor = System.Drawing.Color.Red;
            LblWarning.ForeColor = System.Drawing.Color.Red;
            break;
        case 5:
            LblWarning.Visible = true;
            LblWarning.Text = "* Lütfen Bu Alanları Doldurunuz..";
            LblDepartment.Text = "* Soyad";
            LblDepartment.ForeColor = System.Drawing.Color.Red;
            LblWarning.ForeColor = System.Drawing.Color.Red;
            break;
        case 6:
            LblFingerWarning.Visible = true;
            LblFingerWarning.Text = "Lütfen Parmak İzinizi Üç Kez Veriniz.";
            LblFingerWarning.ForeColor = System.Drawing.Color.Red;
            break;
        default:
            break;
    }

}

However when user click Save button it will enter first IF condition. Then Else If .. But here is my problem. How i can organize these items. For example if user did't fill all boxes i want to give him all warning message not step by step.

You can use a validator for text box. The validator controls that you would find in toolbox.

Try the link

Use if instead of else if because when first if is true it will not enter into else if, so only one message is displayed. Use it as :

     if ((string.IsNullOrEmpty(TbTcNo.Text) || string.IsNullOrWhiteSpace(TbTcNo.Text)))
        {
            warningcase = 1;
            TextLabelManagement(warningcase);                              
        }
        if ((string.IsNullOrEmpty(TbId.Text) || string.IsNullOrWhiteSpace(TbId.Text)))
        {
            warningcase = 2;
            TextLabelManagement(warningcase);               
        }
         if ((string.IsNullOrEmpty(TbName.Text) || string.IsNullOrWhiteSpace(TbName.Text)))
        {
            warningcase = 3;
            TextLabelManagement(warningcase);      
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM