c# - 如何从 foreach 循环中排除特定类型的控件?
问题描述
我正在尝试验证表单控件以查看它们是否为空,并在我的连击中发现了一个有趣的点。
List<string> emptycontrols = new List<string>();
foreach (Control control in Mainform.V_Datafield.Controls)
{
if (control.Text.Contains(null))
{
emptycontrols.Add(control.Name);
}
}
if (emptycontrols.Count > 0)
{
MessageBox.Show("Empty fields detected:", emptycontrols.ToString());
}
以上是我的平庸解决方案,运行时会出现一个控件,即DateTimePicker
控件永远不会为空,而且完全正确。
最终我的问题是我将如何DateTimePicker
从foreach
循环中排除控件,以便它会忽略它但继续检查其他控件?
组框 ( V_datafield
) 包含:
- 10 个文本框
- 1 个富文本框
- 1 x DateTimePicker 如上所述。
解决方案
foreach
您可以随时在循环中进行如下检查
if (control is DateTimePicker)
continue;
推荐阅读
- python - 如何设置特定的 x 轴标签以在熊猫箱线图上显示非字母顺序?
- reactjs - 空间与网格:何时使用?
- c# - ListCollections 与 autoizedcollection
- r - 如何将大文件读入R环境
- python - 在 Window 上下载 Tensorflow 时出现环境错误
- python - 在 OpenCV DNN 模块中缓存加载的模型
- maven - Maven 模块项目在 Maven 中复制模块 JAR(使用 IntelliJ)
- scala - Spark Scala - rdd distinct nullpointerexception
- asp.net-mvc - 引导模式(弹出窗口)在 ASP.NET MVC 中不起作用
- r - 如何找到时间序列中缺失的时间序列或不规则性?