本文介绍了如何使用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);
}
}
推荐答案
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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!