从foreach C#中排除一些textox [英] Exclude some textoxes from foreach C#

查看:84
本文介绍了从foreach C#中排除一些textox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有以下代码检查空文本框,如果为空则返回消息。

但其中一些不是强制性的。

如何从检查中仅排除2个文本框?其中一些是在分组框中。



感谢您的帮助



我尝试过什么:



Hi,

I have the code below to check for empty textboxes and return a message if empty.
But some of them are not mandatory.
How can i exclude only 2 textboxes from checking? some of them are in groupboxes.

Thanks for the help

What I have tried:

foreach (var textBox in Controls.OfType<GroupBox>().SelectMany(groupBox => groupBox.Controls.OfType<TextBox>()))

{
    if (textBox is TextBox && textBox.Visible && string.IsNullOrEmpty(textBox.Text))
    {
        MessageBox.Show($"A value is missing!", "Warning!");
        textBox.Focus();
        return;
    }
}

推荐答案

< span class =code-string>缺少值!, 警告!);
textBox.Focus();
return ;
}
}
"A value is missing!", "Warning!"); textBox.Focus(); return; } }


如果我理解你的问题,我不会把这个代码放在表格中。



我创建一个新的TextBox控件来实现验证代码和一个设置,告诉它是否应该运行。您所要做的就是创建一个继承自TextBox的类。使用新的TextBox代替要在表单上验证的TextBox。如果值通过验证,每个都可以检查自己的值并返回true / false。



你可以从简单的东西开始,如下所示:

If I understand your problem correctly, I wouldn't be putting this code in the form.

I'd create a new TextBox control that implements the validation code and a setting telling it if it should even run. All you have to do is create a class that inherits from TextBox. Use your new TextBox in place of the TextBoxes you want to validate on your form. Each of these can check its own value and return a true/false if the value passes validation.

You could start with something simple, like this:
public class ValidatingTextBox : TextBox
{
    public bool ShouldValidate { get; set; }

    public bool IsValueOk()
    {
        // Set out default return value. In this case, if we don't care
        // if validation runs or not, the default should be true.
        bool returnValue = true;

        if (ShouldValidate)
        {
            // your rules for validating the content go here.
            if (!string.IsNullOrWhitespace(Text))
            {
                // some other rules to validate the value.

                // if one of the rules fails, set the return value to false.
                returnValue = false;
            }
        }

        return returnValue;
    }
}



现在您不必根据标签值过滤掉。您可以通过将其ShouldValidate属性设置为true来告诉每个ValidatingTextBox它是应该验证自身还是不简单。


Now you don't have to filter out based on tag value. You can just tell each ValidatingTextBox whether it should validate itself or not simple by setting its ShouldValidate property to true.


假设这两个文本框的名称是txtbox3和txtbox4

它可以在两个地方完成,首先放在SelectMany之后的谓词。



当前代码是



Suppose name of those two text boxes are txtbox3 and txtbox4
It can be done in two places first put where predicate after SelectMany.

Current code is

foreach (var textBox in Controls.OfType<GroupBox>().SelectMany(groupBox => groupBox.Controls.OfType<TextBox>()))







这应该是





var idsToExclude = new [] {txtbox3,txtbox4};




this should be


var idsToExclude = new []{"txtbox3","txtbox4"};

foreach (var textBox in Controls.OfType<GroupBox>().SelectMany(groupBox => groupBox.Controls.OfType<textbox>()).Where(t=> !idsToExclude.Contains(t.Name) ))</textbox>





如果条件



Also you could this logic in if condition

你也可以使用这个逻辑


这篇关于从foreach C#中排除一些textox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆