1、常用调用方法
  1. public partial class UCRights : UserControl
  2. {
  3. private readonly int LOCATIONY;
  4. private DataTable MENU = new DataTable();
  5. private BLL.User oUser = new HRPOWER.BLL.User();
  6.  
  7. public UCRights()
  8. {
  9. InitializeComponent();
  10. LOCATIONY = panRightMenu_Top.Location.Y;
  11. }
  12.  
  13. private void UCRights_Load(object sender, EventArgs e)
  14. {
  15. tebDesc.Enabled = false;
  16. tebDesc.BackColor = Color.White;
  17. tebPWD.PasswordChar = '*';
  18. tebPWD2.PasswordChar = '*';
  19.  
  20. MENU.Columns.Add("MENUID");
  21. MENU.Columns.Add("USERID");
  22. MENU.Columns.Add("COMPANYID");
  23. MENU.AcceptChanges();
  24.  
  25. BLL.User.UploadValues2CobUID(cobID);
  26.  
  27. SetTabSort();
  28.  
  29. LoadCompanyComponent();
  30. }
  31.  
  32. /// <summary>
  33. /// 加载公司组件
  34. /// </summary>
  35. private void LoadCompanyComponent()
  36. {
  37. HRPOWER.BLL.Company _ocompany = new HRPOWER.BLL.Company();
  38. DataTable _dt = _ocompany.GetCompanyInfo();
  39.  
  40. int LOCATIONX = ;
  41.  
  42. if (_dt.Rows.Count > )
  43. {
  44. for (int i = ; i < _dt.Rows.Count; i++)
  45. {
  46. Parts.UCRightCompany _ucRC = new HRPOWER.WIN.UC.Parts.UCRightCompany();
  47. _ucRC.COMPANYNAME = _dt.Rows[i]["NAME_SHORT"].ToString();
  48. _ucRC.COMPANYID = _dt.Rows[i]["ID"].ToString();
  49. Control c = (Control)_ucRC;
  50. c.Location = new Point(LOCATIONX, LOCATIONY);
  51. c.Name = "UCRightCompany" + i.ToString();
  52. this.Controls.Add(c);
  53. LOCATIONX += ;
  54. }
  55. }
  56. if (LOCATIONX > this.Width)
  57. {
  58. this.Width = LOCATIONX + _dt.Rows.Count;
  59. }
  60. }
  61.  
  62. /// <summary>
  63. /// 获取用户对应的权限
  64. /// </summary>
  65. /// <param name="cContainer">控件容器</param>
  66. private void SearchControls(System.Windows.Forms.Control cContainer)
  67. {
  68. foreach (System.Windows.Forms.Control c in cContainer.Controls)
  69. {
  70. if (c is CheckBox)
  71. {
  72. CheckBox _chb = (CheckBox)c;
  73. if (_chb.Checked)
  74. {
  75. string _sCompID = String.Empty;
  76. Control[] ctlList = _chb.Parent.Parent.Controls.Find("chbCompany", true);
  77.  
  78. if (ctlList.Length > )
  79. {
  80. CheckBox chb = (CheckBox)ctlList.GetValue();
  81. _sCompID = chb.Text;
  82.  
  83. if (MENU.Select("MENUID=" + _chb.Tag.ToString()).Length > )
  84. {
  85. DataRow dr = MENU.Select("MENUID=" + _chb.Tag.ToString())[];
  86. DataRow ddr = MENU.NewRow();
  87. ddr["USERID"] = dr["USERID"];
  88. ddr["MENUID"] = dr["MENUID"];
  89. ddr["COMPANYID"] = dr["COMPANYID"].ToString().Contains(_sCompID) ? dr["COMPANYID"].ToString() : dr["COMPANYID"].ToString() + _sCompID + ",";
  90. MENU.Rows.Remove(dr);
  91. MENU.Rows.Add(ddr);
  92.  
  93. }
  94. else
  95. {
  96. DataRow ddr = MENU.NewRow();
  97. ddr["USERID"] = cobID.Text;
  98. ddr["MENUID"] = _chb.Tag.ToString();
  99. ddr["COMPANYID"] = _sCompID + ",";
  100. MENU.Rows.Add(ddr);
  101. }
  102. MENU.AcceptChanges();
  103. }
  104. }
  105. }
  106. if (c.HasChildren) SearchControls(c);
  107. }
  108. }
  109.  
  110. private void butSave_Click(object sender, EventArgs e)
  111. {
  112. String _sResult = String.Empty;
  113. if (!CheckInfoIsFull())
  114. {
  115. return;
  116. }
  117.  
  118. SearchControls(this);
  119.  
  120. _sResult = oUser.UpdateRoleInfoByUser(cobID.Text, tebName.Text, tebPWD.Text, MENU);
  121.  
  122. if (_sResult.ToLower() == "true")
  123. {
  124. BLL.BaseClass.DialogMessage("您好,用户权限设置成功!", "提示");
  125. tebName.Text = null;
  126. cobID.Text = null;
  127. tebPWD.Text = tebPWD2.Text = null;
  128. SetCheckBoxIsUnchecked(this);
  129. }
  130. else
  131. {
  132. BLL.BaseClass.DialogMessage("抱歉,用户权限设置不成功,请确认信息无误后再设置", "提示");
  133. BLL.BaseClass.LogErrorLog(_sResult);
  134. tebName.Focus();
  135. }
  136.  
  137. MENU.Clear();
  138. }
  139.  
  140. /// <summary>
  141. /// 检测必选条件是否符合要求,true为是,false为否
  142. /// </summary>
  143. /// <returns></returns>
  144. private Boolean CheckInfoIsFull()
  145. {
  146. Boolean _bRtn = true;
  147.  
  148. if (String.IsNullOrEmpty(tebName.Text.Trim()))
  149. {
  150. BLL.BaseClass.DialogMessage(String.Format("您好,{0}不能为空", labName.Text.Replace(" ", String.Empty)), "提示");
  151. tebName.Focus();
  152. _bRtn = false;
  153. }
  154. if (tebName.Text.Trim().Length > )
  155. {
  156. BLL.BaseClass.DialogMessage(String.Format("抱歉,{0}长度不能超过50个字符", labName.Text.Replace(" ", String.Empty)), "提示");
  157. tebName.Focus();
  158. _bRtn = false;
  159. }
  160. if (String.IsNullOrEmpty(cobID.Text.Trim()))
  161. {
  162. BLL.BaseClass.DialogMessage(String.Format("您好,{0}不能为空", labID.Text.Replace(" ", String.Empty)), "提示");
  163. cobID.Focus();
  164. _bRtn = false;
  165. }
  166. if (cobID.Text.Trim().Length > || !BLL.BaseClass.IsNumberOrChar(cobID.Text.Trim()))
  167. {
  168. BLL.BaseClass.DialogMessage(String.Format("抱歉,{0}长度不能超过50个字符同时只能为数字或字母", labID.Text.Replace(" ", String.Empty)), "提示");
  169. cobID.Focus();
  170. _bRtn = false;
  171. }
  172. if (String.IsNullOrEmpty(tebPWD.Text))
  173. {
  174. BLL.BaseClass.DialogMessage(String.Format("您好,{0}不能为空", labPWD.Text.Replace(" ", String.Empty)), "提示");
  175. tebPWD.Focus();
  176. _bRtn = false;
  177. }
  178. if (String.IsNullOrEmpty(tebPWD2.Text))
  179. {
  180. BLL.BaseClass.DialogMessage(String.Format("您好,{0}不能为空", labPWD2.Text.Replace(" ", String.Empty)), "提示");
  181. tebPWD2.Focus();
  182. _bRtn = false;
  183. }
  184. if (tebPWD.Text.Length> || tebPWD2.Text.Length>)
  185. {
  186. BLL.BaseClass.DialogMessage("抱歉,密码长度不能超过50", "提示");
  187. tebPWD.Focus();
  188. _bRtn = false;
  189. }
  190. if (String.Compare(tebPWD.Text, tebPWD2.Text, false) != || !BLL.BaseClass.IsNumberOrChar(tebPWD.Text.Trim()))
  191. {
  192. BLL.BaseClass.DialogMessage(String.Format("抱歉,{0}与{1}不相同或者密码不为数字、字母组成",
  193. labPWD.Text.Replace(" ", String.Empty),
  194. labPWD2.Text.Replace(" ", String.Empty)), "提示");
  195. tebPWD.Focus();
  196. _bRtn = false;
  197. }
  198.  
  199. return _bRtn;
  200. }
  201.  
  202. /// <summary>
  203. /// 设置WINDOWS TAB键顺序
  204. /// </summary>
  205. private void SetTabSort()
  206. {
  207. cobID.TabIndex = ;
  208. tebName.TabIndex = ;
  209. tebPWD.TabIndex = ;
  210. tebPWD2.TabIndex = ;
  211. butSave.TabIndex = ;
  212. cobID.Focus();
  213. }
  214.  
  215. private void cobID_SelectedIndexChanged(object sender, EventArgs e)
  216. {
  217. SetCheckBoxIsUnchecked(this);
  218. DataTable _dtMenu = oUser.GetUserMenuInfoByUID(cobID.Text);
  219. if (_dtMenu.Rows.Count > )
  220. {
  221. tebName.Text = _dtMenu.Rows[]["Comment"].ToString();
  222. tebPWD2.Text = tebPWD.Text = BLL.BaseClass.Decrypt(_dtMenu.Rows[]["Password"].ToString());
  223. foreach (DataRow dr in _dtMenu.Rows)
  224. {
  225. SetValueToCheckBox(this, dr);
  226. }
  227. }
  228. else
  229. {
  230. tebName.Text = tebPWD.Text = tebPWD2.Text = null;
  231. }
  232. }
  233.  
  234. /// <summary>
  235. /// 根据查询结果,给用户设置对应的值
  236. /// </summary>
  237. /// <param name="cContainer">控件容器</param>
  238. /// <param name="dr">DataRow</param>
  239. private void SetValueToCheckBox(Control cContainer, DataRow dr)
  240. {
  241. foreach (Control c in cContainer.Controls)
  242. {
  243. if (c is CheckBox)
  244. {
  245. CheckBox chb = (CheckBox)c;
  246. string tmp = dr["MENU_ID"].ToString();
  247. string tmp1 = chb.Tag.ToString();
  248. if (tmp1 == tmp && !chb.Checked)
  249. {
  250. //查找公司CheckBox控件
  251. Control[] cList = chb.Parent.Parent.Controls.Find("chbCompany", true);
  252. if (cList.Length > )
  253. {
  254. if (dr["COMPANY_ID"].ToString().Contains(((CheckBox)cList.GetValue()).Text))
  255. {
  256. chb.Checked = true;
  257. }
  258. }
  259. }
  260. }
  261.  
  262. if (c.HasChildren)
  263. {
  264. SetValueToCheckBox(c, dr);
  265. }
  266. }
  267. }
  268.  
  269. /// <summary>
  270. /// 设置CheckBox控件为未选状态
  271. /// </summary>
  272. /// <param name="cContainer">控件容器</param>
  273. private void SetCheckBoxIsUnchecked(Control cContainer)
  274. {
  275. foreach (Control c in cContainer.Controls)
  276. {
  277. if (c is CheckBox)
  278. {
  279. ((CheckBox)c).Checked = false;
  280. }
  281.  
  282. //处理隐藏的Panel不被处理
  283. if (c.HasChildren && c.Name!="panCompany0Hide")
  284. SetCheckBoxIsUnchecked(c);
  285. }
  286. }
  287.  
  288. private void cobID_KeyPress(object sender, KeyPressEventArgs e)
  289. {
  290. if (e.KeyChar == (char))
  291. {
  292. cobID_SelectedIndexChanged(null, null);
  293. }
  294. }
  295.  
  296. }

2、获取指定窗体内,所以check值集合  

  1. /// <summary>
  2. /// 加载公司组件
  3. /// </summary>
  4. private void LoadCompanyComponent()
  5. {
  6. HRPOWER.BLL.Company _ocompany = new HRPOWER.BLL.Company();
  7. DataTable _dt = _ocompany.GetCompanyInfo();
  8.  
  9. int LOCATIONX = ;
  10.  
  11. if (_dt.Rows.Count > )
  12. {
  13. for (int i = ; i < _dt.Rows.Count; i++)
  14. {
  15. Parts.UCRightCompany _ucRC = new HRPOWER.WIN.UC.Parts.UCRightCompany();
  16. _ucRC.COMPANYNAME = _dt.Rows[i]["NAME_SHORT"].ToString();
  17. _ucRC.COMPANYID = _dt.Rows[i]["ID"].ToString();
  18. Control c = (Control)_ucRC;
  19. c.Location = new Point(LOCATIONX, LOCATIONY);
  20. c.Name = "UCRightCompany" + i.ToString();
  21. this.Controls.Add(c);
  22. LOCATIONX += ;
  23. }
  24. }
  25. if (LOCATIONX > this.Width)
  26. {
  27. this.Width = LOCATIONX + _dt.Rows.Count;
  28. }
  29. }
  30.  
  31. /// <summary>
  32. /// 获取用户对应的权限
  33. /// </summary>
  34. /// <param name="cContainer">控件容器</param>
  35. private void SearchControls(System.Windows.Forms.Control cContainer)
  36. {
  37. foreach (System.Windows.Forms.Control c in cContainer.Controls)
  38. {
  39. if (c is CheckBox)
  40. {
  41. CheckBox _chb = (CheckBox)c;
  42. if (_chb.Checked)
  43. {
  44. string _sCompID = String.Empty;
  45. Control[] ctlList = _chb.Parent.Parent.Controls.Find("chbCompany", true);
  46.  
  47. if (ctlList.Length > )
  48. {
  49. CheckBox chb = (CheckBox)ctlList.GetValue();
  50. _sCompID = chb.Text;
  51.  
  52. if (MENU.Select("MENUID=" + _chb.Tag.ToString()).Length > )
  53. {
  54. DataRow dr = MENU.Select("MENUID=" + _chb.Tag.ToString())[];
  55. DataRow ddr = MENU.NewRow();
  56. ddr["USERID"] = dr["USERID"];
  57. ddr["MENUID"] = dr["MENUID"];
  58. ddr["COMPANYID"] = dr["COMPANYID"].ToString().Contains(_sCompID) ? dr["COMPANYID"].ToString() : dr["COMPANYID"].ToString() + _sCompID + ",";
  59. MENU.Rows.Remove(dr);
  60. MENU.Rows.Add(ddr);
  61.  
  62. }
  63. else
  64. {
  65. DataRow ddr = MENU.NewRow();
  66. ddr["USERID"] = cobID.Text;
  67. ddr["MENUID"] = _chb.Tag.ToString();
  68. ddr["COMPANYID"] = _sCompID + ",";
  69. MENU.Rows.Add(ddr);
  70. }
  71. MENU.AcceptChanges();
  72. }
  73. }
  74. }
  75. if (c.HasChildren) SearchControls(c);
  76. }
  77. }

WinForm 遍历用户控件里CheckBox的更多相关文章

  1. winfrom获取用户控件里的控件对象

    如何获取用户控件里的控件对象呢,其实思路也是很简单的, 比如有一个panel 用户控件 里面有许多的其他控件. 那么要找出一个Label控件怎么找呢,好的.现在我们就开始 首先,一个foreach循环 ...

  2. Winform开发常用控件之Checkbox和CheckedListBox

    Winform的开发基本都是基于控件事件的,也就是事件驱动型的. 多选框的放置和值的获取有很多种,这里介绍几个简单常用的方法 1.直接放置Checkbox,并获取Checkbox的值 上图 做法也非常 ...

  3. (c#)WinForm遍历所有控件

    遍历WinForm中的所有控件,只执行一次以下语句是不行的 foreach (Control ctl in this.Controls) { } 这样只能遍历到第一层控件,子控件是遍历不到的,要想遍历 ...

  4. C#winform窗体用户控件自定义事件

    C#许多事情都和事件有关系,大部分的事情我们可以通过C#自己的事件来完成,但如果我们自己新建了一个自定义控件,我们该如何定义自己想要的事件呢?下面我就来为大家粗略的讲解一番. 假设我们自定义了一个控件 ...

  5. 在SharePoint 2010 母版页里添加自定义用户控件

    在SharePoint 2010 母版页里添加自定义用户控件(译) 使用自定义用户控件的好处: 1.容易部署:2.易于控制显示或隐藏. (在使用的过程中)可能要面对的问题是:如何在用户控件里使用Sha ...

  6. (九)ASP.NET自定义用户控件(2)

    http://www.cnblogs.com/SkySoot/archive/2012/09/04/2670678.html 用户控件 在 .NET 里,可以通过两种方式把自己的控件插入到 Web 窗 ...

  7. Asp.net 恢复页面内用户控件内的控件ClientID

    众所周知在Asp.net中如果一个页面添加了一个用户控件(或母版页),那么用户控件内的控件的   ClientID号会被自动添加页面中用户控件的ClientID 即页面中的控件内的控件ClientID ...

  8. ASP.NET的用户控件

    本文介绍如何在ASP.NET中创建用户控件,控件属性的动态修改以及控件的事件出发机制. 简介ASP.NET的服务端控件使得Web开发工作变得更为简单,功能更为强大.我们介绍过如何在ASP.NET页面中 ...

  9. WPF窗口和用户控件事件相互触发

    问题1: WPF项目里有一个窗口和一个用户控件,窗口和用户控件里都有一个Button,点击窗口里的Button如何触发用户控件里Button的Click事件 解答: //窗口代码 public par ...

随机推荐

  1. 让PDF.NET支持最新的SQLite数据库

    最近项目中用到了SQLite,之前项目中用的是PDF.NET+MySQL的组合,已经写了不少代码,如果能把写好的代码直接用在SQLite上就好了,PDF.NET支持大部分主流的数据库,这个当然可以,只 ...

  2. iOS之UITableView组头组尾视图/标题悬停

    最近笔者在公司的iOS开发中,有一个iOS开发同事跑来问了两个问题:1.给UITableView设置了组头和组尾视图,但是一直显示不出来?2.UITableView的section的header和fo ...

  3. C#事务

    看了很多关于事务的概念,还是觉得维基百科上说的最好: 数据库事务(简称:事务)是数据库管理系统执行过程中的一个逻辑单位,由一个有限的数据库操作序列构成. 一个数据库事务通常包含了一个序列的对数据库的读 ...

  4. pyMysql

    本篇对于Python操作MySQL主要使用两种方式: 原生模块 pymsql ORM框架 SQLAchemy pymsql pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb ...

  5. Linux下使用Hexo搭建github博客

    找到一篇靠谱的博客,备份一下: ---------以下原文------------------ Nodejs安装 因为hexo是基于nodejs的应用,所以要先安装nodejs才可以.我这里以Ubun ...

  6. 使用hexo,如果换了电脑怎么更新博客?

    自己今天想到这个问题,于是去知乎搜索了一番,发现不甚理想.没找到合适的,题目就是知乎原题.只好自己解决了.以下直接把自己的答案粘贴过来 今天我突然想到这个问题,想来参考参考,却发现都不太适合我.首先, ...

  7. python collections模块

    collections模块基本介绍 collections在通用的容器dict,list,set和tuple之上提供了几个可选的数据类型 namedtuple() factory function f ...

  8. c# 面向方面编程

    AOP面向切面编程(Aspect Oriented Programming),是通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.Spring框架用的核心技术就是AOP,是函数式编程的一 ...

  9. Bubble Cup 8 finals I. Robots protection (575I)

    题意: 有一个正方形区域, 要求支持两个操作: 1.放置三角形,给定放置方向(有4个方向,直角边与坐标轴平行),直角顶点坐标,边长 2.查询一个点被覆盖了多少次 1<=正方形区域边长n<= ...

  10. TeamViewer 12.0.71503 Patch By.Sound

    TeamViewer - the All-In-One Software for Remote Support and Online Meetings - Remote control any com ...