如何使用foreach将MaskedTextBox'ValidatingType设置为typeof(DateTime) [英] how use a foreach to set MaskedTextBox'ValidatingType to typeof(DateTime)

查看:69
本文介绍了如何使用foreach将MaskedTextBox'ValidatingType设置为typeof(DateTime)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





i想要使用foreach将MaskedTextBox'ValidatingType设置为typeof(DateTime)





hi

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


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);
            }
        }

推荐答案

我打算在这里叫它一晚(03) :格林尼治标准时间+00的00,但我想帮助你开始这个,同时它是新鲜的。请回答我对你的问题的评论中提出的问题。



首先,如果你真的需要找到你的表格或其他控制容器中的每个MaskedTextBox,不无论封装级别有多深层嵌套,你都需要使用递归。



这是你可以做到这一点的很多方法的一个例子;为简单起见,我不会在这里使用Linq,我将使用标准风格的递归而不是使用堆栈:
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:
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;
}


这篇关于如何使用foreach将MaskedTextBox'ValidatingType设置为typeof(DateTime)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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