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);
}
}