删除标签文本(如果已存在)

时间:2017-01-16 16:29:49

标签: c# winforms label

我希望在winforms应用程序中添加一项功能,允许员工使用以EMP开头的徽章登录(例如)。我有四个标签,最多四名员工。使用下面的代码,如果员工1(标签1)签出,然后员工4尝试退出,员工4填写员工1的位置,因为它按顺序进行。我确信有一个简单的方法可以做到这一点而不使用所有这些if语句我只是遇到了一些麻烦。有人能指出我正确的方向吗?

if (EmployeeOneLabel.Text == "" && ScanEntryTextBox.Text.Contains(EmployeePrefix)) 
    {
        EmployeeOneLabel.Text = ScanEntryTextBox.Text;
        EmployeeOneSaved = EmployeeOneLabel.Text;
        ScanEntryTextBox.Text = "";
    } else if(ScanEntryTextBox.Text == EmployeeOneSaved)
    {   
        EmployeeOneLabel.Text = "";
        ScanEntryTextBox.Text = "";
    }
    if (EmployeeTwoLabel.Text == "" && ScanEntryTextBox.Text.Contains(EmployeePrefix))
    {
        EmployeeTwoLabel.Text = ScanEntryTextBox.Text;
        EmployeeTwoSaved = EmployeeTwoLabel.Text;
        ScanEntryTextBox.Text = "";
    } else if (ScanEntryTextBox.Text == EmployeeTwoSaved)
    {   
        EmployeeTwoLabel.Text = "";
        ScanEntryTextBox.Text = "";
    }
    if (EmployeeThreeLabel.Text == "" && ScanEntryTextBox.Text.Contains(EmployeePrefix))
    {
        EmployeeThreeLabel.Text = ScanEntryTextBox.Text;
        EmployeeThreeSaved = EmployeeThreeLabel.Text;
        ScanEntryTextBox.Text = "";
    } else if (ScanEntryTextBox.Text == EmployeeThreeSaved)
    {
        EmployeeThreeLabel.Text = "";
        ScanEntryTextBox.Text = "";
    }
    if (EmployeeFourLabel.Text == "" && ScanEntryTextBox.Text.Contains(EmployeePrefix))
    {
        EmployeeFourLabel.Text = ScanEntryTextBox.Text;
        EmployeeFourSaved = EmployeeFourLabel.Text;
        ScanEntryTextBox.Text = "";
    } else if (ScanEntryTextBox.Text == EmployeeFourSaved)
    {
        EmployeeFourLabel.Text = "";
        ScanEntryTextBox.Text = "";
    }

基于@PaulF建议的新更新代码。

private string EmployeePrefix = "EMP";
List<string> listEmployee = new List<string>();
public TimeTrack()
{
    InitializeComponent();
}
private void EnterButton_Click(object sender, EventArgs e)
{
    Label[] LabelEmployee = { EmployeeOneLabel, EmployeeTwoLabel, EmployeeThreeLabel, EmployeeFourLabel };
    if (ScanEntryTextBox.Text.Contains(EmployeePrefix))
    {
        if (listEmployee.Contains(ScanEntryTextBox.Text))
        {
            listEmployee.Remove(ScanEntryTextBox.Text);
            ScanEntryTextBox.Text = "";
        } else
        {
            listEmployee.Add(ScanEntryTextBox.Text);
            ScanEntryTextBox.Text = "";
        }
        for (int i = 0; i < LabelEmployee.Length; i++)
        {
            LabelEmployee[i].Text = listEmployee.ElementAtOrDefault(i) ?? "";
            ScanEntryTextBox.Text = "";
        }
    }
}

Moving label text up

1 个答案:

答案 0 :(得分:2)

如何使用字符串列表。

当您获得有效的员工徽章时 - 检查它是否在列表中&amp;删除它,否则将其添加到列表的末尾。

然后使用新列表内容更新标签。

类似的东西:

List<string> listEmployee = new List<string>();

...
...

if (ScanEntryTextBox.Text.Contains(EmployeePrefix) && (listEmployee.Count < 4))
{
  if (listEmployee.Contains(ScanEntryTextBox.Text))
    listEmployee.Remove(ScanEntryTextBox.Text);
  else
    listEmployee.Add(ScanEntryTextBox.Text);
  EmployeeOneLabel.Text = listEmployee.ElementAtOrDefault(0) ?? "";
  EmployeeTwoLabel.Text = listEmployee.ElementAtOrDefault(1) ?? "";
  EmployeeThreeLabel.Text = listEmployee.ElementAtOrDefault(2) ?? "";
  EmployeeFourLabel.Text = listEmployee.ElementAtOrDefault(3) ?? "";
}

你可能会创建一系列标签&amp;使用for循环来更新标签。

Label [] labelEmployee = {EmployeeOneLabel, EmployeeTwoLabel, EmployeeThreeLabel, EmployeeFourLabel};
List<string> listEmployee = new List<string>();

...
...

if (ScanEntryTextBox.Text.Contains(EmployeePrefix) && (listEmployee.Count < labelEmployee.Length))
{
  if (listEmployee.Contains(ScanEntryTextBox.Text))
    listEmployee.Remove(ScanEntryTextBox.Text);
  else
    listEmployee.Add(ScanEntryTextBox.Text);
  for (int i = 0; i < labelEmployee.Length; i++)
    labelEmployee[i].Text = listEmployee.ElementAtOrDefault(i) ?? "";
}