Click here to Skip to main content
15,895,817 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
hi

i want use foreach to set MaskedTextBox'ValidatingType to typeof(DateTime)


C#
foreach(Control item in this.Controls)
        {
            foreach (Control co in item.Controls)
            {
                if(co is MaskedTextBox)
                {
                    MaskedTextBox mtb = (MaskedTextBox)co;
                    mtb.ValidatingType = typeof(DateTime);
                }
            }
            if (item is MaskedTextBox)
            {
                MaskedTextBox mtb1 = (MaskedTextBox)item;
                mtb1.ValidatingType = typeof(DateTime);
            }
        }
Posted
Comments
BillWoodruff 19-Nov-14 14:05pm    
And, what is the problem you have now with this code ? If it not working, tell us what's happening when you run it: what are any message errors, where do errors occur.

I don't see any implementation of 'TypeValidationCompleted EventHandlers; I don't see any value assigned for the 'Mask Property of the MaskedTextBoxes.
Sergey Alexandrovich Kryukov 19-Nov-14 14:06pm    
First of all, note one of the purposes of the masked text box is to avoid validation, or at least avoid excessive validation...
—SA
BillWoodruff 19-Nov-14 14:28pm    
There are issues specific to using MaskedTextBox where you want DateTime entry which do require special handling beyond just setting the 'ValidatingType property to typeof(DateTime). We don't see any evidence ... yet ... the OP has started addressing the requirements of that special handling, but let's give them a chance to respond.
Sergey Alexandrovich Kryukov 19-Nov-14 15:35pm    
Well, sure...
—SA

1 solution

I'm about to call it a night here (03:00 at GMT+7), but I'd like to help you get started on this while it's fresh in mind. Please respond to the issues raised in my comment on your question.

First, if you really need to locate every MaskedTextBox in your Form, or other Container of Controls, no matter how deeply nested the level of encapsulation may be, you need to use recursion.

Here's one example of many ways you could do this; for simplicity, I won't use Linq here, and I'll use standard-flavor recursion rather than use a Stack:
C#
private readonly Type dateTimeType = typeof(DateTime);

private List<MaskedTextbox> allMaskedTextBoxes = new List<MaskedTextbox>(); 

private void SetMaskTBValType(Control.ControlCollection cCollection)
{
    foreach (Control c in cCollection)
    {
        if (c is MaskedTextBox)
        {
            var toMaskType = c as MaskedTextBox;

            toMaskType.ValidatingType = dateTimeType;

            // implement the other things you will need here !
        }
        else
        {
            if (c.HasChildren)
            {
                SetMaskTBValType(c.Controls);
            }
        }
    }
}

// and here's how you would call it to set the 
// ValidationType for every MaskedTextBox on the Form
private void YourForm_Load(object sender, EventArgs e)
{
    SetMaskTBValType(this.Controls);

    // verify
    bool test = maskedTextBox1.ValidatingType == dateTimeType;
}
 
Share this answer
 
v2
Comments
Manas Bhardwaj 19-Nov-14 15:25pm    
Yes +5!
Sergey Alexandrovich Kryukov 19-Nov-14 15:37pm    
5ed, but you screed up formatting; and I suggest you fix it. It happens due to automatic post processing because you forgot to escape angular brackets. This is the reason of wrong casing of the maskedtextbox (generic parameter).
—SA
BillWoodruff 19-Nov-14 15:56pm    
Thanks Sergey, I pasted in working code: I have never seen the CP editor munge 'MaskedTextBox into lower-case before !
Sergey Alexandrovich Kryukov 19-Nov-14 16:26pm    
You are welcome. All you need to avoid it is to escape HTML characters like angular brackets.
—SA
Sergey Alexandrovich Kryukov 19-Nov-14 16:27pm    
You are welcome. All you need to avoid it is to escape such HTML/XML characters as angular brackets.
—SA

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