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)
posted @ 2017-06-23 14:43 yangykaifa 阅读(...) 评论(...) 编辑 收藏

var allowComments=true,cb_blogId=347936,cb_entryId=7069837,cb_blogApp=currentBlogApp,cb_blogUserGuid='46672cd6-b11e-e711-9fc1-ac853d9f53cc',cb_entryCreatedDate='2017/6/23 14:43:00';loadViewCount(cb_entryId);var cb_postType=1;var isMarkdown=false;

var commentManager = new blogCommentManager();commentManager.renderComments(0);

var googletag = googletag || {};
googletag.cmd = googletag.cmd || [];

googletag.cmd.push(function() {
googletag.defineSlot('/1090369/C1', [300, 250], 'div-gpt-ad-1546353474406-0').addService(googletag.pubads());
googletag.defineSlot('/1090369/C2', [468, 60], 'div-gpt-ad-1539008685004-0').addService(googletag.pubads());
googletag.pubads().enableSingleRequest();
googletag.enableServices();
});

if(enablePostBottom()) {
codeHighlight();
fixPostBody();
setTimeout(function () { incrementViewCount(cb_entryId); }, 50);
deliverT2();
deliverC1();
deliverC2();
loadNewsAndKb();
loadBlogSignature();
LoadPostInfoBlock(cb_blogId, cb_entryId, cb_blogApp, cb_blogUserGuid);
GetPrevNextPost(cb_entryId, cb_blogId, cb_entryCreatedDate, cb_postType);
loadOptUnderPost();
GetHistoryToday(cb_blogId, cb_blogApp, cb_entryCreatedDate);
}

WinForm容器内控件批量效验是否同意为空?设置是否仅仅读?设置是否可用等方法分享的更多相关文章

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

    WinForm容器内控件批量效验是否允许为空?设置是否只读?设置是否可用等方法分享 在WinForm程序中,我们有时需要对某容器内的所有控件做批量操作.如批量判断是否允许为空?批量设置为只读.批量设置 ...

  2. Winform禁止容器内控件获得焦点时改变容器显示范围坐标

    在Winform中当容器的可视高度无法显示所有控件并且容器的AutoScroll属性设置为True的情况下,但点击容器内某个未显示完整的控件时,会出现容器的滚动条自动下滚的情况. 这是由于控件获得焦点 ...

  3. c# WinForm开发 DataGridView控件的各种操作总结(单元格操作,属性设置)

    一.单元格内容的操作 *****// 取得当前单元格内容 Console.WriteLine(DataGridView1.CurrentCell.Value); // 取得当前单元格的列 Index ...

  4. 转:c# WinForm开发 DataGridView控件的各种操作总结(单元格操作,属性设置)

    一.单元格内容的操作 *****// 取得当前单元格内容 Console.WriteLine(DataGridView1.CurrentCell.Value); // 取得当前单元格的列 Index  ...

  5. WinForm窗体及其控件的自适应

    3步骤: 1.在需要自适应的Form中实例化全局变量   AutoSizeFormClass.cs源码在下方 AutoSizeFormClass asc = new AutoSizeFormClass ...

  6. 转:C# WinForm窗体及其控件的自适应

    一.说明 2012-11-30 曾经写过 <C# WinForm窗体及其控件自适应各种屏幕分辨率>  ,其中也讲解了控件自适应的原理.近期有网友说,装在panel里面的控件,没有效果? 这 ...

  7. C# LIstbox 解决WinForm下ListBox控件“设置DataSource属性后无法修改项集合”的问题

    解决WinForm下ListBox控件“设置DataSource属性后无法修改项集合”的问题 分类: winform2008-05-24 02:33 2592人阅读 评论(11) 收藏 举报 winf ...

  8. DevExpress winform XtraEditor常用控件

    最近在公司里面开始使用DevExpress winform的第三方控件进行开发和维护,这里整理一些常用控件的资料以便于后续查看 ComboBoxEdit 这个控件和winform自带的控件差不多,使用 ...

  9. Winform中checklistbox控件的常用方法

    Winform中checklistbox控件的常用方法最近用到checklistbox控件,在使用其过程中,收集了其相关的代码段1.添加项checkedListBox1.Items.Add(" ...

随机推荐

  1. BZOJ4031——HEOI小z的房间

    题意:求某网格图生成树个数,对1e9取模 题解:题目是裸的Matrix-Tree定理,这不是我要说的重点,重点是对于这个取模的处理. 由于这不是个质数,所以不能直接乘逆元来当除法用.直接高斯消元肯定是 ...

  2. Sqoop_具体总结 使用Sqoop将HDFS/Hive/HBase与MySQL/Oracle中的数据相互导入、导出

    一.使用Sqoop将MySQL中的数据导入到HDFS/Hive/HBase watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYWFyb25oYWRvb3A=/ ...

  3. 0x27 A*

    终于完全了解A*到底是什么玩意儿了 对于当前的决策,选取当前花费+预估花费最小来拓展. 因为假如预估出现失误,那么很可能就会延伸到一个错误的决策点,而这个决策点偏偏就是ed,而由于预估失误,其他点的当 ...

  4. edit filter rules in sql source control

    https://documentation.red-gate.com/soc6/common-tasks/exclude-objects-using-filters 如果有人上传了filter,nam ...

  5. 英语发音规则---G字母

    英语发音规则---G字母 一.总结 一句话总结: 1.G发[g]音? bag [bæg] n. 袋:猎获物 go [gəʊ] vi. 走:达到 garden ['gɑːd(ə)n] n. 花园 gla ...

  6. MVC/MVP/MVVM区别——MVVM就是angular,视图和数据双向绑定

    摘自:http://www.ruanyifeng.com/blog/2015/02/mvcmvp_mvvm.html 一.MVC MVC模式的意思是,软件可以分成三个部分. 视图(View):用户界面 ...

  7. pyspark MLlib踩坑之model predict+rdd map zip,zip使用尤其注意啊啊啊!

    Updated:use model broadcast, mappartition+flatmap,see: from pyspark import SparkContext import numpy ...

  8. Linux,Docker,Jenkins No such file or directory

    你们先休息下,我先哭哭! 今天在做交接项目的bug修改的时候,在创建文件的时候报错 No such file or directory 然后跟着路径去linux中查看了该路径,但确实存在,并且权限都是 ...

  9. Spring学习笔记(二) 初探Spring

    版权声明 笔记出自<Spring 开发指南>一书. Spring 初探 前面我们简单介绍了 Spring 的基本组件和功能,现在我们来看一个简单示例: Person接口Person接口定义 ...

  10. jQuery分页插件pagination的用法

    https://www.zhangxinxu.com/jq/pagination_zh/ 参数: 参数名 描述 参数值 maxentries 总条目数 必选参数,整数 items_per_page 每 ...