/// <summary>
/// 判断一容器控件内某控件的值是否可以为空(通过控件的AccessibleName属性设置为"NotNull")
/// <remarks>
/// 说明:
/// 此方法显示提示信息,对于相应取值不能为空的控件,应设置其“Tag”属性,以友好提示信息。
/// </remarks>
/// </summary>
/// <param name="parContainer">容器控件</param>
public
static
bool
ControlValueIsEmpty(Control parContainer)
{
bool
returnValue =
true
;
string
hintInfo =
string
.Empty;
for
(
int
index = 0; index < parContainer.Controls.Count; index++)
{
if
(parContainer.Controls[index].HasChildren && !parContainer.Controls[index].GetType().Name.ToLower().StartsWith(
"uc"
))
{
ControlValueIsEmpty(parContainer.Controls[index]);
}
else
{
if
(
string
.IsNullOrEmpty(parContainer.Controls[index].AccessibleName))
{
continue
;
}
if
(!parContainer.Controls[index].AccessibleName.ToLower().Contains(
"notnull"
)
&& !parContainer.Controls[index].GetType().Name.ToLower().Contains(
"mask"
))
{
continue
;
}
switch
(parContainer.Controls[index].GetType().Name)
{
case
"Label"
:
break
;
case
"ComboBox"
:
case
"ComboBoxEx"
:
case
"UcComboBoxEx"
:
if
(parContainer.Controls[index]
is
ComboBox)
{
if
(((ComboBox)(parContainer.Controls[index])).Text.Trim() ==
string
.Empty)
{
hintInfo += GetControlName((ComboBox)parContainer.Controls[index]) +
"\n"
;
returnValue =
false
;
}
}
else
{
if
(((UcComboBoxEx)(parContainer.Controls[index])).Text.Trim() ==
string
.Empty)
{
hintInfo += GetControlName((UcComboBoxEx)parContainer.Controls[index]) +
"\n"
;
returnValue =
false
;
}
}
break
;
case
"TextBox"
:
case
"UcTextBox"
:
if
(parContainer.Controls[index]
is
TextBox)
{
if
(((TextBox)(parContainer.Controls[index])).Text.Trim() ==
string
.Empty)
{
hintInfo += GetControlName((TextBox)parContainer.Controls[index]) +
"\n"
;
returnValue =
false
;
}
}
else
{
if
(((UcTextBox)(parContainer.Controls[index])).Text.Trim() ==
string
.Empty)
{
hintInfo += GetControlName((UcTextBox)parContainer.Controls[index]) +
"\n"
;
returnValue =
false
;
}
}
break
;
case
"RichTextBox"
:
if
(((RichTextBox)(parContainer.Controls[index])).Text.Trim() ==
string
.Empty)
{
hintInfo += GetControlName((RichTextBox)parContainer.Controls[index]) +
"\n"
;
returnValue =
false
;
}
break
;
case
"MaskedTextBox"
:
case
"UcMaskTextBox"
:
string
mskTxtValue =
string
.Empty;
object
controlChinaeseName =
null
;
if
(parContainer.Controls[index]
is
MaskedTextBox)
{
mskTxtValue = ((MaskedTextBox)(parContainer.Controls[index])).Text;
controlChinaeseName = ((MaskedTextBox)(parContainer.Controls[index])).Tag ?? ((MaskedTextBox)(parContainer.Controls[index])).Name;
}
else
{
mskTxtValue = ((UcMaskTextBox)(parContainer.Controls[index])).Text;
controlChinaeseName = ((UcMaskTextBox)(parContainer.Controls[index])).Tag ?? ((UcMaskTextBox)(parContainer.Controls[index])).Name;
}
if
(mskTxtValue.Substring(0, 4).Trim().Length > 0)
{
if
(DateTimeHelper.IsDate(mskTxtValue))
{
if
(DateTimeHelper.ToDate(mskTxtValue) < DateTimeHelper.ToDate(
"1754-01-01"
) ||
DateTimeHelper.ToDate(mskTxtValue) >= DateTimeHelper.ToDate(
"9999-12-31"
))
{
MessageBoxHelper.ShowErrorMsg(
"["
+ controlChinaeseName +
"] 日期范围不正确! /n正确日期范围为:1754-01-01 至 9999-12-31"
);
returnValue =
false
;
}
}
else
{
MessageBoxHelper.ShowErrorMsg(
"["
+ controlChinaeseName +
"] 日期格式不正确! 正确格式如:2012-01-01"
);
returnValue =
false
;
}
}
else
{
if
(mskTxtValue.Substring(0, 5).Equals(
" -"
) && parContainer.Controls[index].AccessibleName.ToLower() ==
"notnull"
)
{
MessageBoxHelper.ShowErrorMsg(
"["
+ controlChinaeseName +
"]不能为空!"
);
returnValue =
false
;
}
}
break
;
default
:
break
;
}
}
}
if
(!
string
.IsNullOrEmpty(hintInfo.Trim()))
{
MessageBoxHelper.ShowWarningMsg(hintInfo +
"不能为空!"
);
}
return
returnValue;
}
private
static
string
GetControlName(Control ctr)
{
if
(ctr.Tag ==
null
)
{
return
ctr.Name;
}
else
{
return
ctr.Tag.ToString();
}
}
private
static
void
ShowInfo(Control ctr,
string
info)
{
if
(ctr.Tag ==
null
)
{
MessageBoxHelper.ShowWarningMsg(ctr.Name + info);
}
else
{
MessageBoxHelper.ShowWarningMsg(ctr.Tag + info);
}
}
相关推荐
本例中,为WinForm提供方法,批量设置其界面上所有的控件为不可编辑状态 方法: SetReadOnly( Control.ControlCollection , List, Boolean>> , List) 说明: 当窗体内的控件存在[ReadOnly]属性时,只设置...
WinForm自定义分页控件完整实例,导入可用
winForm皮肤&控件winForm皮肤&控件winForm皮肤&控件winForm皮肤&控件
如果窗体里面控件,尤其是TextBox控件比较多的时候,代码里面是不是要设置每个控件的Text属性值为空呢?下面我用这个例子,来探讨下清空界面控件值的小技巧。而且还能复用。不必每个窗体都做这些枯燥、无聊的工作。 ...
winform的自定义控件,可以在设计时选择资源窗口
非常好看的winform 自定义日期控件
该源码为.NET Winform分页控件DevExpress版,源码有调用实例
在winform中设置控件的背景图,美化你的UI界面
Winform调用WPF控件,演示WPF的DataGrid控件在Winform中如何调用刷新
C# Winform自定义电池控件 进度条
winform 自定义日期控件
3.为窗体添加SizeChanged事件,并在其方法Form1_SizeChanged中,首次记录窗体和其控件初始位置和大小,之后调用类的自适应方法,完成自适应。 4.Panel,DataGridView等控件同样适用。 应用代码如下: using System; ...
winform图片轮播控件
winform 自定义日期控件--最低分分享
C#代码高亮的WinForm的TEXTBOX控件C#代码高亮的WinForm的TEXTBOX控件C#代码高亮的WinForm的TEXTBOX控件C#代码高亮的WinForm的TEXTBOX控件C#代码高亮的WinForm的TEXTBOX控件C#代码高亮的WinForm的TEXTBOX控件C#代码...
在WinForm项目开发中习惯于对于集合数据的批量修改,再一次性提交更新同步到数据库。这里我们就必须实现对对象的改变的跟踪记录,我们实现对象的改变跟踪有许多方式,大致我尝试了两种方式:1:对象强制实现接口,...
Winform C# Flash控件 属性 方法 事件 ShockwaveFlashObjects 介绍
Winform开发界面UI控件库开源共享,Winform开发界面UI控件库开源共享