WinForm容器内控件批量效验是否允许为空?设置是否只读?设置是否可用等方法分享

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
容器服务 Serverless 版 ACK Serverless,952元额度 多规格
容器服务 Serverless 版 ACK Serverless,317元额度 多规格
简介: 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chinahuyong/article/details/47395633 Wi...
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chinahuyong/article/details/47395633

WinForm容器内控件批量效验是否允许为空?设置是否只读?设置是否可用等方法分享

 

  在WinForm程序中,我们有时需要对某容器内的所有控件做批量操作、如批量判断是否允许为空?批量设置为只读、批量设置为可用或不可用等常用操作,本文分享这几种方法,起抛砖引玉的作用,欢迎讨论!

 1、  清除容器控件内里面指定控件的值的方法

/// <summary>
/// 清除容器里面指定控件的值(通过控件的AccessibleName属性设置为"EmptyValue")
/// </summary>
/// <param name="parContainer">容器控件</param>
public  static  void  EmptyControlValue(Control parContainer)
{
     for  ( int  index = 0; index < parContainer.Controls.Count; index++)
     {
         //如果是容器类控件,递归调用自己
         if  (parContainer.Controls[index].HasChildren && !parContainer.Controls[index].GetType().Name.ToLower().StartsWith( "uc" ))
         {
             EmptyControlValue(parContainer.Controls[index]);
         }
         else
         {
             if  (parContainer.Controls[index].AccessibleName == null  ||
                 !parContainer.Controls[index].AccessibleName.ToLower().Contains( "emptyvalue" ))
             {
                 continue ;
             }
 
             switch  (parContainer.Controls[index].GetType().Name)
             {
                 case  "Label" :
                     break ;
                 //case "ComboBox":
                 //    ((ComboBox)(parContainer.Controls[index])).Text = "";                          
                 //    break;
                 case  "TextBox" :
                     ((TextBox)(parContainer.Controls[index])).Text = "" ;
                     break ;
                 case  "UcTextBox" :
                     ((UcTextBox)(parContainer.Controls[index])).Text = "" ;
                     break ;
                 case  "RichTextBox" :
                     ((RichTextBox)(parContainer.Controls[index])).Text = "" ;
                     break ;
                 case  "MaskedTextBox" :
                     ((MaskedTextBox)(parContainer.Controls[index])).Text = "" ;
                     break ;
                 case  "UcMaskTextBox" :
                     ((UcMaskTextBox)(parContainer.Controls[index])).Text = "" ;
                     break ;
                 case  "RadioButton" :
                     ((RadioButton)(parContainer.Controls[index])).Checked = false ;
                     break ;
                 case  "CheckBox" :
                     ((CheckBox)(parContainer.Controls[index])).Checked = false ;
                     break ;
             }
         }
     }
}

  

  要清空控件的值、只需调用:  

EmptyControlValue(容器控件名称);

 2、断一容器控件内某控件的值是否可以为空?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/// <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" : //排除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" ;
                             //ShowInfo((ComboBox)parContainer.Controls[index], " 不能为空!");
                             //((ComboBox)(parContainer.Controls[index])).Focus();
                             returnValue =  false ;
                         }
                     }
                     else
                     {
                         if  (((UcComboBoxEx)(parContainer.Controls[index])).Text.Trim() ==  string .Empty)
                         {
                             hintInfo += GetControlName((UcComboBoxEx)parContainer.Controls[index]) +  "\n" ;
                             //ShowInfo((UcComboBoxEx)parContainer.Controls[index], " 不能为空!");
                             //((UcComboBoxEx)(parContainer.Controls[index])).Focus();
                             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" ;
                             //ShowInfo((TextBox)parContainer.Controls[index], " 不能为空!");
                             //((TextBox)(parContainer.Controls[index])).Focus();
                             returnValue =  false ;
                         }
                     }
                     else
                     {
                         if  (((UcTextBox)(parContainer.Controls[index])).Text.Trim() ==  string .Empty)
                         {
                             hintInfo += GetControlName((UcTextBox)parContainer.Controls[index]) +  "\n" ;
                             //ShowInfo((UcTextBox)parContainer.Controls[index], " 不能为空!");
                             //((UcTextBox)(parContainer.Controls[index])).Focus();
                             returnValue =  false ;
                         }
                     }
                     break ;
                 case  "RichTextBox" :
                     if  (((RichTextBox)(parContainer.Controls[index])).Text.Trim() ==  string .Empty)
                     {
                         hintInfo += GetControlName((RichTextBox)parContainer.Controls[index]) +  "\n" ;
                         //ShowInfo((RichTextBox)parContainer.Controls[index], " 不能为空!");
                         //((RichTextBox)(parContainer.Controls[index])).Focus();
                         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))
                         {
                             //把用户输入的日期数据控制在(1754-01-01 至 9999-12-31这间),这主要解决SqlServer与C#日期范围的冲突
                             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);
     }
}

  方法“ControlValueIsEmpty”可以用于批量判断指定容器内的所有控件是否可以为空,对于不为空的可以做批量提示显示,设置如下图所示:

 3、设置容器控件中包含的控件为只读?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/// <summary>
/// 设置容器控件中包含的控件为只读(通过控件的AccessibleName属性设置为"CanReadOnly")
/// </summary>
/// <param name="parContainer">容器控件</param>
/// <param name="isReadOnly">是否为只读,true是只读,false则相反</param>>
public  static  void  SetControlReadOnly(Control parContainer,  bool  isReadOnly)
{
     for  ( int  index = 0; index < parContainer.Controls.Count; index++)
     {
         //如果是容器类控件,递归调用自己
         if  (parContainer.Controls[index].HasChildren)
         {
             SetControlReadOnly(parContainer.Controls[index], isReadOnly);
         }
         else
         {
             if  (parContainer.Controls[index].AccessibleName ==  null  &&
               !parContainer.Controls[index].AccessibleName.ToLower().Contains( "canreadonly" ))
             {
                 continue ;
             }
 
             switch  (parContainer.Controls[index].GetType().Name)
             {
                 case  "TextBox" :
                 case  "UcTextBox" :
                     if  (parContainer.Controls[index]  is  TextBox)
                     {
                         ((TextBox)(parContainer.Controls[index])).ReadOnly = isReadOnly;
                     }
                     else
                     {
                         ((UcTextBox)(parContainer.Controls[index])).ReadOnly = isReadOnly;
                     }
 
                     break ;
                 case  "RichTextBox" :
                     ((RichTextBox)(parContainer.Controls[index])).ReadOnly = isReadOnly;
                     break ;
                 case  "MaskedTextBox" :
                 case  "UcMaskTextBox" :
                     if  (parContainer.Controls[index]  is  MaskedTextBox)
                     {
                         ((MaskedTextBox)(parContainer.Controls[index])).ReadOnly = isReadOnly;
                     }
                     else
                     {
                         ((UcMaskTextBox)(parContainer.Controls[index])).ReadOnly = isReadOnly;
                     }
                     break ;
                 case  "ComboBox" :
                     ((ComboBox)(parContainer.Controls[index])).Enabled = !isReadOnly;
                     break ;
                 case  "Button" :
                 case  "UcButton" :
                     if  (parContainer.Controls[index]  is  Button)
                     {
                         ((Button)(parContainer.Controls[index])).Enabled = !isReadOnly;
                     }
                     else
                     {
                         ((UcButton)(parContainer.Controls[index])).Enabled = !isReadOnly;
                     }
                     break ;
                 default :
                     break ;
             }
         }
     }
}

  方法“SetControlReadOnly”的使用方式与上面的方法相同,只要设置控件的“AccessibleName”属性为“CanReadOnly”即可。

 4、设置容器控件中包含的控件是否可用?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/// <summary>
/// 设置容器控件中包含的控件是否可用(通过控件的AccessibleName属性设置为"Enabled")
/// </summary>
/// <param name="parContainer">容器控件</param>
/// <param name="isEnabled">是否为用可,true:可用,false:不可用</param>>
public  static  void  SetControlEnabled(Control parContainer,  bool  isEnabled)
{
     for  ( int  index = 0; index < parContainer.Controls.Count; index++)
     {
         //如果是容器类控件,递归调用自己
         if  (parContainer.Controls[index].HasChildren)
         {
             SetControlEnabled(parContainer.Controls[index], isEnabled);
         }
         else
         {
             if  (parContainer.Controls[index].AccessibleName ==  null  &&
                !parContainer.Controls[index].AccessibleName.ToLower().Contains( "Enabled" ))
             {
                 continue ;
             }
 
             //(parContainer.Controls[index]).BackColor = System.Drawing.Color.White;//设置当前控件的背景色为白色
 
             switch  (parContainer.Controls[index].GetType().Name)
             {
                 case  "Label" :
                     break ;
                 default :
                     parContainer.Controls[index].Enabled = isEnabled;
                     break ;
             }
         }
     }
}

  方法“SetControlEnabled”用于设置容器控件内的指定控件的Enabled属性。

  同时需要说明的时,这些方法可以同时设置,只需要设置控件的“AccessibleName”为这种类型即可:EmptyValue| NotNull |Enabled|CanReadOnly,这样设置即可,对于提示信息的显示,我们可以设置控件的Tag属性。

 

  欢迎关注RDIFramework.NET框架官方公众微信(微信号:guosisoft),及时了解最新动态。

   扫描二维码立即关注

 

作者: EricHu 
出处:http://www.cnblogs.com/huyong/ 
Email:406590790@qq.com 
QQ交流:406590790 
框架官网:http://www.rdiframework.net/ 
框架官网博客:http://blog.rdiframework.net/ 
框架其他博客:http://blog.csdn.net/chinahuyong 
               http://www.cnblogs.com/huyong
RDIFramework.NET,基于.NET的快速信息化系统开发、整合框架,给用户和开发者最佳的.Net框架部署方案。 
关于作者:高级工程师、信息系统项目管理师、DBA。专注于微软平台项目架构、管理和企业解决方案,多年项目开发与管理经验,曾多次组织并开发多个大型项目,在面向对象、面向服务以及数据库领域有一定的造诣。现主要从事基于 RDIFramework.NET框架的技术开发、咨询工作,主要服务于金融、医疗卫生、铁路、电信、物流、物联网、制造、零售等行业。 
如有问题或建议,请多多赐教! 
本文版权归作者和CNBLOGS博客共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,如有问题,可以通过邮箱或QQ 联系我,非常感谢。

相关文章
|
5月前
|
Prometheus Kubernetes 监控
容器服务ACK常见问题之pod设置securityContext调整参数失败如何解决
容器服务ACK(阿里云容器服务 Kubernetes 版)是阿里云提供的一种托管式Kubernetes服务,帮助用户轻松使用Kubernetes进行应用部署、管理和扩展。本汇总收集了容器服务ACK使用中的常见问题及答案,包括集群管理、应用部署、服务访问、网络配置、存储使用、安全保障等方面,旨在帮助用户快速解决使用过程中遇到的难题,提升容器管理和运维效率。
|
11天前
|
移动开发 前端开发 HTML5
Twaver-HTML5基础学习(20)数据容器(3)_数据的批量加载(节省性能方法)
本文介绍了Twaver HTML5中数据的批量加载方法,通过使用`box.startBatch()`可以在大量数据加载时提高性能。文章通过示例代码展示了如何在React组件中使用批量加载功能,以减少界面重绘次数并提升效率。
26 1
Twaver-HTML5基础学习(20)数据容器(3)_数据的批量加载(节省性能方法)
|
8天前
|
Shell Docker 容器
10-19|使用date命令: 你可以在容器内使用date命令来设置时间,但为了防止这个更改影响宿主机,你不能以特权模式运行容器。我没有加特权模式的时候,使用此命令告诉我没权限啊
10-19|使用date命令: 你可以在容器内使用date命令来设置时间,但为了防止这个更改影响宿主机,你不能以特权模式运行容器。我没有加特权模式的时候,使用此命令告诉我没权限啊
|
2月前
|
容器
【Qt 学习笔记】Qt常用控件 | 容器类控件 | Group Box的使用及说明
【Qt 学习笔记】Qt常用控件 | 容器类控件 | Group Box的使用及说明
126 3
|
2月前
|
容器
【Qt 学习笔记】Qt常用控件 | 容器类控件 | Tab Widget的使用及说明
【Qt 学习笔记】Qt常用控件 | 容器类控件 | Tab Widget的使用及说明
26 2
|
2月前
|
存储 容器
容器镜像解析问题之desc.Image() 方法确定返回的 Image 接口类型如何解决
容器镜像解析问题之desc.Image() 方法确定返回的 Image 接口类型如何解决
15 0
|
3月前
|
Ubuntu Devops 云计算
ubuntu docker-compose编排容器并且设置自启动
使用Docker Compose编排容器并设置为Ubuntu系统的自启动服务,不仅优化了应用的部署流程,也提升了运行时的可管理性和可靠性。通过上述步骤,您可以轻松实现这一目标。维护此类服务时,记得定期检查和更新您的 `docker-compose.yml`文件,确保所有的服务都符合当前的业务需求和技术标准。在云计算和微服务架构不断演进的今天,掌握Docker Compose等工具对于DevOps和软件工程师来说,变得尤为重要。
140 3
|
3月前
|
JavaScript 前端开发 容器
vue组件封装——固定宽高比的容器(2种方法:纯CSS实现 + JS实现)
vue组件封装——固定宽高比的容器(2种方法:纯CSS实现 + JS实现)
109 2
|
5月前
|
容器 内存技术
Qt中常用容器组控件介绍和实操-1
Qt中常用容器组控件介绍和实操
|
5月前
|
API 容器
Qt中常用容器组控件介绍和实操-2
Qt中常用容器组控件介绍和实操
下一篇
无影云桌面