简体   繁体   English

如何在C#中管理Label

[英]How to manage Label in c#

I am trying to organize Label and TextBoxes in c# i have two function : 我正在尝试在c#中组织Label和TextBoxes我有两个功能:

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. 在这里,我写一些如果TextBox为null的情况,我将给出错误消息以填充它们。 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. 但是,当用户单击“保存”按钮时,它将进入第一个IF条件。 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. 使用if代替else if,因为当first if为true时,if不会进入else,因此仅显示一条消息。 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);      
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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