1、常用调用方法
  public partial class UCRights : UserControl
{
private readonly int LOCATIONY;
private DataTable MENU = new DataTable();
private BLL.User oUser = new HRPOWER.BLL.User(); public UCRights()
{
InitializeComponent();
LOCATIONY = panRightMenu_Top.Location.Y;
} private void UCRights_Load(object sender, EventArgs e)
{
tebDesc.Enabled = false;
tebDesc.BackColor = Color.White;
tebPWD.PasswordChar = '*';
tebPWD2.PasswordChar = '*'; MENU.Columns.Add("MENUID");
MENU.Columns.Add("USERID");
MENU.Columns.Add("COMPANYID");
MENU.AcceptChanges(); BLL.User.UploadValues2CobUID(cobID); SetTabSort(); LoadCompanyComponent();
} /// <summary>
/// 加载公司组件
/// </summary>
private void LoadCompanyComponent()
{
HRPOWER.BLL.Company _ocompany = new HRPOWER.BLL.Company();
DataTable _dt = _ocompany.GetCompanyInfo(); int LOCATIONX = ; if (_dt.Rows.Count > )
{
for (int i = ; i < _dt.Rows.Count; i++)
{
Parts.UCRightCompany _ucRC = new HRPOWER.WIN.UC.Parts.UCRightCompany();
_ucRC.COMPANYNAME = _dt.Rows[i]["NAME_SHORT"].ToString();
_ucRC.COMPANYID = _dt.Rows[i]["ID"].ToString();
Control c = (Control)_ucRC;
c.Location = new Point(LOCATIONX, LOCATIONY);
c.Name = "UCRightCompany" + i.ToString();
this.Controls.Add(c);
LOCATIONX += ;
}
}
if (LOCATIONX > this.Width)
{
this.Width = LOCATIONX + _dt.Rows.Count;
}
} /// <summary>
/// 获取用户对应的权限
/// </summary>
/// <param name="cContainer">控件容器</param>
private void SearchControls(System.Windows.Forms.Control cContainer)
{
foreach (System.Windows.Forms.Control c in cContainer.Controls)
{
if (c is CheckBox)
{
CheckBox _chb = (CheckBox)c;
if (_chb.Checked)
{
string _sCompID = String.Empty;
Control[] ctlList = _chb.Parent.Parent.Controls.Find("chbCompany", true); if (ctlList.Length > )
{
CheckBox chb = (CheckBox)ctlList.GetValue();
_sCompID = chb.Text; if (MENU.Select("MENUID=" + _chb.Tag.ToString()).Length > )
{
DataRow dr = MENU.Select("MENUID=" + _chb.Tag.ToString())[];
DataRow ddr = MENU.NewRow();
ddr["USERID"] = dr["USERID"];
ddr["MENUID"] = dr["MENUID"];
ddr["COMPANYID"] = dr["COMPANYID"].ToString().Contains(_sCompID) ? dr["COMPANYID"].ToString() : dr["COMPANYID"].ToString() + _sCompID + ",";
MENU.Rows.Remove(dr);
MENU.Rows.Add(ddr); }
else
{
DataRow ddr = MENU.NewRow();
ddr["USERID"] = cobID.Text;
ddr["MENUID"] = _chb.Tag.ToString();
ddr["COMPANYID"] = _sCompID + ",";
MENU.Rows.Add(ddr);
}
MENU.AcceptChanges();
}
}
}
if (c.HasChildren) SearchControls(c);
}
} private void butSave_Click(object sender, EventArgs e)
{
String _sResult = String.Empty;
if (!CheckInfoIsFull())
{
return;
} SearchControls(this); _sResult = oUser.UpdateRoleInfoByUser(cobID.Text, tebName.Text, tebPWD.Text, MENU); if (_sResult.ToLower() == "true")
{
BLL.BaseClass.DialogMessage("您好,用户权限设置成功!", "提示");
tebName.Text = null;
cobID.Text = null;
tebPWD.Text = tebPWD2.Text = null;
SetCheckBoxIsUnchecked(this);
}
else
{
BLL.BaseClass.DialogMessage("抱歉,用户权限设置不成功,请确认信息无误后再设置", "提示");
BLL.BaseClass.LogErrorLog(_sResult);
tebName.Focus();
} MENU.Clear();
} /// <summary>
/// 检测必选条件是否符合要求,true为是,false为否
/// </summary>
/// <returns></returns>
private Boolean CheckInfoIsFull()
{
Boolean _bRtn = true; if (String.IsNullOrEmpty(tebName.Text.Trim()))
{
BLL.BaseClass.DialogMessage(String.Format("您好,{0}不能为空", labName.Text.Replace(" ", String.Empty)), "提示");
tebName.Focus();
_bRtn = false;
}
if (tebName.Text.Trim().Length > )
{
BLL.BaseClass.DialogMessage(String.Format("抱歉,{0}长度不能超过50个字符", labName.Text.Replace(" ", String.Empty)), "提示");
tebName.Focus();
_bRtn = false;
}
if (String.IsNullOrEmpty(cobID.Text.Trim()))
{
BLL.BaseClass.DialogMessage(String.Format("您好,{0}不能为空", labID.Text.Replace(" ", String.Empty)), "提示");
cobID.Focus();
_bRtn = false;
}
if (cobID.Text.Trim().Length > || !BLL.BaseClass.IsNumberOrChar(cobID.Text.Trim()))
{
BLL.BaseClass.DialogMessage(String.Format("抱歉,{0}长度不能超过50个字符同时只能为数字或字母", labID.Text.Replace(" ", String.Empty)), "提示");
cobID.Focus();
_bRtn = false;
}
if (String.IsNullOrEmpty(tebPWD.Text))
{
BLL.BaseClass.DialogMessage(String.Format("您好,{0}不能为空", labPWD.Text.Replace(" ", String.Empty)), "提示");
tebPWD.Focus();
_bRtn = false;
}
if (String.IsNullOrEmpty(tebPWD2.Text))
{
BLL.BaseClass.DialogMessage(String.Format("您好,{0}不能为空", labPWD2.Text.Replace(" ", String.Empty)), "提示");
tebPWD2.Focus();
_bRtn = false;
}
if (tebPWD.Text.Length> || tebPWD2.Text.Length>)
{
BLL.BaseClass.DialogMessage("抱歉,密码长度不能超过50", "提示");
tebPWD.Focus();
_bRtn = false;
}
if (String.Compare(tebPWD.Text, tebPWD2.Text, false) != || !BLL.BaseClass.IsNumberOrChar(tebPWD.Text.Trim()))
{
BLL.BaseClass.DialogMessage(String.Format("抱歉,{0}与{1}不相同或者密码不为数字、字母组成",
labPWD.Text.Replace(" ", String.Empty),
labPWD2.Text.Replace(" ", String.Empty)), "提示");
tebPWD.Focus();
_bRtn = false;
} return _bRtn;
} /// <summary>
/// 设置WINDOWS TAB键顺序
/// </summary>
private void SetTabSort()
{
cobID.TabIndex = ;
tebName.TabIndex = ;
tebPWD.TabIndex = ;
tebPWD2.TabIndex = ;
butSave.TabIndex = ;
cobID.Focus();
} private void cobID_SelectedIndexChanged(object sender, EventArgs e)
{
SetCheckBoxIsUnchecked(this);
DataTable _dtMenu = oUser.GetUserMenuInfoByUID(cobID.Text);
if (_dtMenu.Rows.Count > )
{
tebName.Text = _dtMenu.Rows[]["Comment"].ToString();
tebPWD2.Text = tebPWD.Text = BLL.BaseClass.Decrypt(_dtMenu.Rows[]["Password"].ToString());
foreach (DataRow dr in _dtMenu.Rows)
{
SetValueToCheckBox(this, dr);
}
}
else
{
tebName.Text = tebPWD.Text = tebPWD2.Text = null;
}
} /// <summary>
/// 根据查询结果,给用户设置对应的值
/// </summary>
/// <param name="cContainer">控件容器</param>
/// <param name="dr">DataRow</param>
private void SetValueToCheckBox(Control cContainer, DataRow dr)
{
foreach (Control c in cContainer.Controls)
{
if (c is CheckBox)
{
CheckBox chb = (CheckBox)c;
string tmp = dr["MENU_ID"].ToString();
string tmp1 = chb.Tag.ToString();
if (tmp1 == tmp && !chb.Checked)
{
//查找公司CheckBox控件
Control[] cList = chb.Parent.Parent.Controls.Find("chbCompany", true);
if (cList.Length > )
{
if (dr["COMPANY_ID"].ToString().Contains(((CheckBox)cList.GetValue()).Text))
{
chb.Checked = true;
}
}
}
} if (c.HasChildren)
{
SetValueToCheckBox(c, dr);
}
}
} /// <summary>
/// 设置CheckBox控件为未选状态
/// </summary>
/// <param name="cContainer">控件容器</param>
private void SetCheckBoxIsUnchecked(Control cContainer)
{
foreach (Control c in cContainer.Controls)
{
if (c is CheckBox)
{
((CheckBox)c).Checked = false;
} //处理隐藏的Panel不被处理
if (c.HasChildren && c.Name!="panCompany0Hide")
SetCheckBoxIsUnchecked(c);
}
} private void cobID_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char))
{
cobID_SelectedIndexChanged(null, null);
}
} }

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

         /// <summary>
/// 加载公司组件
/// </summary>
private void LoadCompanyComponent()
{
HRPOWER.BLL.Company _ocompany = new HRPOWER.BLL.Company();
DataTable _dt = _ocompany.GetCompanyInfo(); int LOCATIONX = ; if (_dt.Rows.Count > )
{
for (int i = ; i < _dt.Rows.Count; i++)
{
Parts.UCRightCompany _ucRC = new HRPOWER.WIN.UC.Parts.UCRightCompany();
_ucRC.COMPANYNAME = _dt.Rows[i]["NAME_SHORT"].ToString();
_ucRC.COMPANYID = _dt.Rows[i]["ID"].ToString();
Control c = (Control)_ucRC;
c.Location = new Point(LOCATIONX, LOCATIONY);
c.Name = "UCRightCompany" + i.ToString();
this.Controls.Add(c);
LOCATIONX += ;
}
}
if (LOCATIONX > this.Width)
{
this.Width = LOCATIONX + _dt.Rows.Count;
}
} /// <summary>
/// 获取用户对应的权限
/// </summary>
/// <param name="cContainer">控件容器</param>
private void SearchControls(System.Windows.Forms.Control cContainer)
{
foreach (System.Windows.Forms.Control c in cContainer.Controls)
{
if (c is CheckBox)
{
CheckBox _chb = (CheckBox)c;
if (_chb.Checked)
{
string _sCompID = String.Empty;
Control[] ctlList = _chb.Parent.Parent.Controls.Find("chbCompany", true); if (ctlList.Length > )
{
CheckBox chb = (CheckBox)ctlList.GetValue();
_sCompID = chb.Text; if (MENU.Select("MENUID=" + _chb.Tag.ToString()).Length > )
{
DataRow dr = MENU.Select("MENUID=" + _chb.Tag.ToString())[];
DataRow ddr = MENU.NewRow();
ddr["USERID"] = dr["USERID"];
ddr["MENUID"] = dr["MENUID"];
ddr["COMPANYID"] = dr["COMPANYID"].ToString().Contains(_sCompID) ? dr["COMPANYID"].ToString() : dr["COMPANYID"].ToString() + _sCompID + ",";
MENU.Rows.Remove(dr);
MENU.Rows.Add(ddr); }
else
{
DataRow ddr = MENU.NewRow();
ddr["USERID"] = cobID.Text;
ddr["MENUID"] = _chb.Tag.ToString();
ddr["COMPANYID"] = _sCompID + ",";
MENU.Rows.Add(ddr);
}
MENU.AcceptChanges();
}
}
}
if (c.HasChildren) SearchControls(c);
}
}

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. mysql数据库远程访问指定数据库设置

    grant all privileges on huarenwang.* to 'huarenwang'@'%' IDENTIFIED BY 'huarenwang'; on后面的是指定的数据库to后 ...

  2. 使用django开发博客过程记录4——Category分类视图

    在写点击博客的所属分类,显示所有该分类的文章时真是让我想了好一会,为什么呢?因为我使用的是cbv模式开发的而不是简单的视图处理逻辑的,所以,有些操作会被包装好了,你并不知道它的细节,那么我们今天要实现 ...

  3. sdk开发时,对外暴露的接口封装

    思考,用同步还是异步? 实质就是屏蔽一些东西,让使用者直接传参数 拿结果 而不用关心具体实现 eg.登陆接口 1.定义接口LoginCallBack,两个函数 请求成功和失败 public inter ...

  4. Win7硬盘整数分区一览表

    10G=10245 MB 20G=20482 MB 30G=30726 MB 40G=40963 MB 50G=51208 MB 60G=61444 MB 70G=71681 MB 80G=81926 ...

  5. Java集合之LinkedHashMap

    一.初识LinkedHashMap 上篇文章讲了HashMap.HashMap是一种非常常见.非常有用的集合,但在多线程情况下使用不当会有线程安全问题. 大多数情况下,只要不涉及线程安全问题,Map基 ...

  6. runtime-对成员变量和属性的操作

    成员变量 首先我们来看看成员变量在runtime中是什么样的 在runtime中成员变量是一个objc_ivar类型的结构体,结构体定义如下 struct objc_ivar { char *ivar ...

  7. iOS组件化思路 <转>

    随着应用需求逐步迭代,应用的代码体积将会越来越大,为了更好的管理应用工程,我们开始借助CocoaPods版本管理工具对原有应用工程进行拆分.但是仅仅完成代码拆分还不足以解决业务之间的代码耦合,为了更好 ...

  8. 【About Queue】(待改)

    队列 队列满足FIFO规则,先进先出. C语言代码:(Segmentation fault你大爷(メ ゚皿゚)メ) #include<stdio.h> #include<stdlib ...

  9. 在js中实现邮箱格式的验证

    在js中实现邮箱格式的验证 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><htm ...

  10. spring boot开发入门

    最近学习了spring boot,刚刚入门,整理下