在面板中选择多个文本框 [英] Select several textboxes in a panel
本文介绍了在面板中选择多个文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有几个嵌套在面板中的文本框,我想检查它们是否有文本.虽然我不想这样写我的代码:
I have several textboxes nested in a panel and I want to check if they have text or not. Although I don't want to write my code like this:
if(textbox1.Text != "" && textbox2.Text != "" ...)
{
...
}
有没有什么方法可以自动执行此操作并提高代码本身的总体质量?
Is there any way to automate this and improve the general quality of the code itslef?
推荐答案
这可以通过使用 System 的
.OfType
和 All
扩展方法很容易地完成.Linq
This can be done very easily by using OfType
and All
extension methods from System.Linq
.
var panel = new Panel
{
Size = new Size(500, 500),
BackColor = Color.Red
};
panel.Controls.Add(new TextBox { Text = "Value" });
panel.Controls.Add(new TextBox { Text = "Value2" });
if (panel.Controls.OfType<TextBox>().All(x => !string.IsNullOrEmpty(x.Text)))
{
//Do something
}
if 语句中的代码只有在 TextBoxes 的所有 Text 属性都不为空时才会执行.
The code in the if statement will only execute if all the Text properties of TextBoxes are not empty.
这篇关于在面板中选择多个文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文