表单 C# 复选框

本文关键字:复选框 表单 | 更新日期: 2023-09-27 18:30:41

只需要这样做,以便它将从用户设置 C# 表单阈值的列表(记录)中删除记录

这是我的代码,任何建议或帮助都很棒,它仅适用于 uni 作业,所以还没有学到很多东西谢谢

 private void FindAndDeleteByCash()
 {
      for (int i = 0; i < records.Count; i++)
      {  
           if (CreditCheckBox.Checked == true)
           {
                if (Convert.ToDouble(BalanceRequired.Text) > records[i].CreditBalance)
                {
                     records.Remove(records[i]);
                }
           }
      }    
 }   

表单 C# 复选框

由于每次删除records.Count都会更改,因此for(int i = 0; i < records.Count; i++)出错。如果将内部 for 循环更改为向后循环,则可以对其进行修改:

 private void FindAndDeleteByCash() {
   for (int i = records.Count - 1; i >= 0; --i) // <- Backward loop
     if (CreditCheckBox.Checked && (Convert.ToDouble(BalanceRequired.Text) > records[i].CreditBalance))
       records.Remove(records[i]); // <- records.RemoveAt(i); looks better here
 }    
records.Count在你的

循环中被更改了,所以你可能想把它放在循环之外。

此外,还可以将 if 语句联接到 1:

    private void FindAndDeleteByCash()
    {
        int recCount = records.Count;
        for (int i = 0; i < recCount; i++)
        {  
            if (CreditCheckBox.Checked == true &&
                 Convert.ToDouble(BalanceRequired.Text) > records[i].CreditBalance)
            {
                    records.Remove(records[i]);
            }
         }    
    }   

对于框架 3.5 或更高版本:(未经测试)

 private void FindAndDeleteByCash()
 {
      Double balance = Convert.ToDouble(BalanceRequired.Text);
      if(CreditCheckBox.Checked == true)
      {
          var rec =  records.Where((rec) => rec <= balance) // Using LINQ
          // print 'rec' to verify, it will contain only related result sets.
      }  
 }