您的位置:首页 > 其它

WinForm 遍历用户控件里CheckBox

2016-05-25 00:13 281 查看
1、常用调用方法

/// <summary>
/// 加载公司组件
/// </summary>
private void LoadCompanyComponent()
{
HRPOWER.BLL.Company _ocompany = new HRPOWER.BLL.Company();
DataTable _dt = _ocompany.GetCompanyInfo();

int LOCATIONX = 218;

if (_dt.Rows.Count > 0)
{
for (int i = 0; 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 += 129;
}
}
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 > 0)
{
CheckBox chb = (CheckBox)ctlList.GetValue(0);
_sCompID = chb.Text;

if (MENU.Select("MENUID=" + _chb.Tag.ToString()).Length > 0)
{
DataRow dr = MENU.Select("MENUID=" + _chb.Tag.ToString())[0];
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);
}
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: