C# 检查任何文本框是否为空,并用值填充
我想要实现的是让系统检查是否有任何空白复选框,我在表单中有许多,如果是空白的,则分配一个值“N/a”。我的代码有什么问题?谢谢。您可以使用C# 检查任何文本框是否为空,并用值填充,c#,winforms,C#,Winforms,我想要实现的是让系统检查是否有任何空白复选框,我在表单中有许多,如果是空白的,则分配一个值“N/a”。我的代码有什么问题?谢谢。您可以使用child.GetType()在迭代控件组时获取控件的类型,然后将其与typeof(TextBox)进行比较,这将帮助您从控件集合中筛选文本框。试试这个: private void btnSaveInformation_Click(object sender, EventArgs e) { foreach (Control child
child.GetType()
在迭代控件组时获取控件的类型,然后将其与typeof(TextBox)
进行比较,这将帮助您从控件集合中筛选文本框。试试这个:
private void btnSaveInformation_Click(object sender, EventArgs e)
{
foreach (Control child in Controls)
{
if (child is TextBox)
{
TextBox tb = child as TextBox;
if (string.IsNullOrEmpty(tb.Text))
{
tb.Text = @"N/A";
}
}
}
//var bal = new StudentBal
//{
// FirstName = txtFirstName.Text
//};
//bal.InsertStudent(bal);
}
或者,您可以像下面这样使用迭代过滤集合(假设控件
是控件的集合):
foreach(Controls.OfType().ToList()中的控件子级)
{
文本框tb=(文本框)子项;
if(string.IsNullOrEmpty(tb.Text))
{
tb.Text=@“不适用”;
}
}
您可以使用child.GetType()
在迭代控件组时获取控件的类型,然后将其与typeof(TextBox)
进行比较,这将帮助您从控件集合中筛选文本框。试试这个:
private void btnSaveInformation_Click(object sender, EventArgs e)
{
foreach (Control child in Controls)
{
if (child is TextBox)
{
TextBox tb = child as TextBox;
if (string.IsNullOrEmpty(tb.Text))
{
tb.Text = @"N/A";
}
}
}
//var bal = new StudentBal
//{
// FirstName = txtFirstName.Text
//};
//bal.InsertStudent(bal);
}
或者,您可以像下面这样使用迭代过滤集合(假设控件
是控件的集合):
foreach(Controls.OfType().ToList()中的控件子级)
{
文本框tb=(文本框)子项;
if(string.IsNullOrEmpty(tb.Text))
{
tb.Text=@“不适用”;
}
}
文本框是否放置在面板或其他分组控件中?如果是这样,表单的控件集合将不包含对它们的引用;它们只包含直系子对象(即面板等)
如果它们包含在组控件或面板中,您可能希望执行以下操作:
foreach (Control child in Controls.OfType<TextBox>().ToList())
{
TextBox tb = (TextBox)child;
if (string.IsNullOrEmpty(tb.Text))
{
tb.Text = @"N/A";
}
}
如果您想要一个更健壮的方法,下面显示了获取所有控件列表的不同方法:
foreach (Control child in Controls)
{
if (child.GetType() == typeof(TextBox))
{
TextBox tb = (TextBox)child;
if (string.IsNullOrEmpty(tb.Text))
{
tb.Text = @"N/A";
}
}
}
文本框是放置在面板还是其他分组控件中?如果是这样,表单的控件集合将不包含对它们的引用;它们只包含直系子对象(即面板等) 如果它们包含在组控件或面板中,您可能希望执行以下操作:
foreach (Control child in Controls.OfType<TextBox>().ToList())
{
TextBox tb = (TextBox)child;
if (string.IsNullOrEmpty(tb.Text))
{
tb.Text = @"N/A";
}
}
如果您想要一个更健壮的方法,下面显示了获取所有控件列表的不同方法:
foreach (Control child in Controls)
{
if (child.GetType() == typeof(TextBox))
{
TextBox tb = (TextBox)child;
if (string.IsNullOrEmpty(tb.Text))
{
tb.Text = @"N/A";
}
}
}
我看不出你的代码有什么错误。我怀疑您正在查找的控件是嵌套控件 我建议将层次结构展平,获取所有(嵌套)控件,然后查找特定类型
foreach (Control child in myGroupPanel.Controls)
{
if (child is TextBox) { // your additional code here }
}
static Func GetControls=
(控制)=>控制
.控制
.Cast().SelectMany(x=>GetControls(x))
.Concat(control.Controls.Cast());
现在您可以在代码中使用上面的委托,如下所示
static Func<Control, IEnumerable<Control>> GetControls =
(control) => control
.Controls
.Cast<Control>().SelectMany(x => GetControls(x))
.Concat(control.Controls.Cast<Control>());
foreach(GetControls(this).OfType()中的文本框tb)
{
if(string.IsNullOrEmpty(tb.Text))
{
tb.Text=@“不适用”;
}
}
我看你的代码没有错。我怀疑您正在查找的控件是嵌套控件
我建议将层次结构展平,获取所有(嵌套)控件,然后查找特定类型
foreach (Control child in myGroupPanel.Controls)
{
if (child is TextBox) { // your additional code here }
}
static Func GetControls=
(控制)=>控制
.控制
.Cast().SelectMany(x=>GetControls(x))
.Concat(control.Controls.Cast());
现在您可以在代码中使用上面的委托,如下所示
static Func<Control, IEnumerable<Control>> GetControls =
(control) => control
.Controls
.Cast<Control>().SelectMany(x => GetControls(x))
.Concat(control.Controls.Cast<Control>());
foreach(GetControls(this).OfType()中的文本框tb)
{
if(string.IsNullOrEmpty(tb.Text))
{
tb.Text=@“不适用”;
}
}
您是否尝试过TextBox=(TextBox)child代码>没有错。您的文本框是否在其他控件(如组框或面板)中?哦。它在一个分组框内。应该怎么做?您是否尝试了textb=(TextBox)child代码>没有错。您的文本框是否在其他控件(如组框或面板)中?哦。它在一个分组框内。应该怎么做?文本框的可能副本在一个分组框中,但在不同的面板中。我尝试了你的建议,但仍然没有王牌。快速而肮脏的解决方案是识别文本框的直接父对象,并创建一个可以循环的连接列表。您可以为此使用LINQ的Concat:var controls=panel1.controls.Concat(panel2.controls).Concat(panel3.controls).Concat(…);foreach(控件中的控件子项){…}。这是快速和肮脏的,但一旦你开始添加更多的文本框,是不可扩展的。在这种情况下,你会想要一些像我添加的链接中的东西。好的。谢谢我会研究你提供的链接。这会有点难,因为我不知道LINQ Yet文本框在一个分组框中,但在不同的面板中。我尝试了你的建议,但仍然没有王牌。快速而肮脏的解决方案是识别文本框的直接父对象,并创建一个可以循环的连接列表。您可以为此使用LINQ的Concat:var controls=panel1.controls.Concat(panel2.controls).Concat(panel3.controls).Concat(…);foreach(控件中的控件子项){…}。这是快速和肮脏的,但一旦你开始添加更多的文本框,是不可扩展的。在这种情况下,你会想要一些像我添加的链接中的东西。好的。谢谢我会研究你提供的链接。因为我还不认识林克,所以会有点难