Click here to Skip to main content
15,908,013 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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;
    }
}
Posted
Updated 21-Nov-16 7:25am
v2
Comments
[no name] 21-Nov-16 11:16am    
By name or tag property would be a couple of ways.
Marc-IT 21-Nov-16 11:18am    
Thanks,
Could you provide an example.
[no name] 21-Nov-16 11:25am    
An example of what? And, why do you need to someone to write code for you that checks a property of a control? Isn't that like Programming 101 stuff? Who wrote your existing code for you?
Marc-IT 21-Nov-16 11:28am    
Yeah i´m not what you would call an expert of coding or i wouldnt be here asking for help.
I just wanted an idea to keep me on track but ok.
Thanks anyway...
CPallini 21-Nov-16 11:55am    
See, for instance
http://stackoverflow.com/questions/19775851/ability-to-find-winform-control-via-the-tag-property

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:
C#
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;
    }
}

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.
 
Share this answer
 
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>()))



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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900