C# 检查控件。数据的值
我在表单上有几个不同的控件(文本框、日期时间选择器、掩码文本框),我想检查它们是否包含任何数据。在“保存”按钮的单击事件中,我有以下代码: 这适用于文本框和MaskeDeditBox,但不适用于日期时间选择器。对于这些,我知道我需要检查.Value属性,但我似乎无法从控件对象访问该属性(即“Control.Value==”“| | Control.Value==null”)C# 检查控件。数据的值,c#,winforms,controls,datepicker,errorprovider,C#,Winforms,Controls,Datepicker,Errorprovider,我在表单上有几个不同的控件(文本框、日期时间选择器、掩码文本框),我想检查它们是否包含任何数据。在“保存”按钮的单击事件中,我有以下代码: 这适用于文本框和MaskeDeditBox,但不适用于日期时间选择器。对于这些,我知道我需要检查.Value属性,但我似乎无法从控件对象访问该属性(即“Control.Value==”“| | Control.Value==null”) 我错过了什么明显的东西吗?非常感谢您对我的代码提出的任何修改建议,以允许我检查DateTimePicker值(或只是为了改
我错过了什么明显的东西吗?非常感谢您对我的代码提出的任何修改建议,以允许我检查DateTimePicker值(或只是为了改进代码)。您需要将它们转换为DateTimePicker:
DateTimePicker dtp = control as DateTimePicker;
if(dtp !=null)
{
//here you can access dtp.Value;
}
另外,在代码的第一部分使用String.IsNullOrEmpty(control.Text)。您需要将它们转换为日期时间选择器:
DateTimePicker dtp = control as DateTimePicker;
if(dtp !=null)
{
//here you can access dtp.Value;
}
另外,在代码的第一部分使用String.IsNullOrEmpty(control.Text)。您需要执行以下操作:
foreach(Control control in this.Controls)
{
if (control.Tag == "Required")
{
DateTimePicker dtp = control as DateTimePicker;
if (dtp != null)
{
// use dtp properties.
}
else if (control.Text == "" || control.Text == null)
{
errorProvider.SetError(control, "* Required Field");
bValid = false;
}
else
{
errorProvider.SetError(control, "");
}
}
}
您需要执行以下操作:
foreach(Control control in this.Controls)
{
if (control.Tag == "Required")
{
DateTimePicker dtp = control as DateTimePicker;
if (dtp != null)
{
// use dtp properties.
}
else if (control.Text == "" || control.Text == null)
{
errorProvider.SetError(control, "* Required Field");
bValid = false;
}
else
{
errorProvider.SetError(control, "");
}
}
}
控件
s没有值
属性<例如,code>DateTimePicker,创建它自己独有的属性
不幸的是,没有完全通用的方法从控件
对象的单个循环中处理此问题。你所能做的最好的事情就是这样:
if(control is DateTimePicker)
{
var picker = control as DateTimePicker;
// handle DateTimePicker specific validation.
}
控件
s没有值
属性<例如,code>DateTimePicker,创建它自己独有的属性
不幸的是,没有完全通用的方法从控件
对象的单个循环中处理此问题。你所能做的最好的事情就是这样:
if(control is DateTimePicker)
{
var picker = control as DateTimePicker;
// handle DateTimePicker specific validation.
}
谢谢大家的帮助!我现在让它正常工作了。谢谢大家的帮助!我现在让它正常工作了。