using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace OEMS
{
public partial class AddFunction : Form
{
DataSet ds_checkbox;
DataSet ds_FunctionList;
public AddFunction()
{
InitializeComponent();
}
private void AddFunction_Load(object sender, EventArgs e)
{
try
{
DataSet ds = appCode.DALL.selectAll_Dataset("Users", "s_uid");
if (ds.Tables.Count != 0)
{
if (ds.Tables[0].Rows.Count != 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
cbbUID.Items.Add(ds.Tables[0].Rows[i]["s_uid"].ToString());
}
}
}
ds_FunctionList = appCode.DALL.selectAll_Dataset("FunctionList", "Functions");
createCheckBox(ds_FunctionList, "Functions", 14);
//appCode.others.showMessage_Error(Functions.Tables[0].Rows.Count.ToString());
}
catch (Exception)
{
throw;
}
}
private void createCheckBox(DataSet ds, string filed, int locationX)//这里动态创建checkbox,根据FunctionList表中数量来创建相应的
{
if (ds.Tables.Count != 0)
{
if (ds.Tables[0].Rows.Count != 0)
{
int LocationY = 0;
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
CheckBox ckb = new CheckBox();
ckb.Text = ds.Tables[0].Rows[i]["" + filed + ""].ToString(); //设置复选框的显示值
ckb.Parent = groupBox1; //设置复选框的父容器
LocationY = LocationY + 25; //设置复选框在父容器中的位置的高度为递增25
ckb.Location = new System.Drawing.Point(locationX, LocationY);//设置复选框的位置
ckb.Name = "ckb" + ckb.Text; //设置控件名
CheckBox ckbVisible = new CheckBox();
ckbVisible.Name = "cbVisible" + ckb.Text;
ckbVisible.Parent = groupBox1;
ckbVisible.Location = new System.Drawing.Point(locationX + 165, LocationY);
ckbVisible.Width = 13;
ckbVisible.Enabled = false;
CheckBox ckbEnabled = new CheckBox();
ckbEnabled.Name = "cbEnabled" + ckb.Text;
ckbEnabled.Parent = groupBox1;
ckbEnabled.Location = new System.Drawing.Point(locationX + 222, LocationY);
ckbEnabled.Width = 13;
ckbEnabled.Enabled = false;
}
}
}
}
private void cbbUID_SelectedValueChanged(object sender, EventArgs e)
{
defaultCheckBox();
string cbbValue = cbbUID.SelectedItem.ToString();
ds_checkbox = appCode.DALL.selectAll_Dataset("UsersFunction", "UF_No,s_uid,Functions,Visible,Enabled,State", "s_uid", cbbValue);
for (int i = 0; i < ds_checkbox.Tables[0].Rows.Count; i++)//根据查询的到结果的行数决定循环次数
{
string function = ds_checkbox.Tables[0].Rows[i]["functions"].ToString();//拿出第1行的function字段
bool visibleState = (bool)(ds_checkbox.Tables[0].Rows[i]["Visible"]);//读出状态字段的值
bool EnabledState = (bool)(ds_checkbox.Tables[0].Rows[i]["Enabled"]);
bool state=(bool)(ds_checkbox.Tables[0].Rows[i]["State"]);
foreach (Control control in this.Controls)//遍历该窗体上所有控件
{
if (control is GroupBox)//如果该控件是groupbox
{
if (control.HasChildren)//如果该控件有子控件
{
foreach (Control ctl in control.Controls)//遍历该控件
{
if (ctl.Name != "label2" && ctl.Name != "label3")//如果子控件的名字不是lable,因为该控件下只有lable和checkbox
{
CheckBox cb = (CheckBox)ctl;//转化该子控件为checkbox
if (ctl.Name == "ckb" + function)//如果该控件checkbox的名字为查询的到function字段+"ckb",前面生成的时候是这样命名的
{
cb.Checked = state;//修改其属性
}
if (ctl.Name=="cbVisible"+function)
{
cb.Checked = visibleState;//修改其属性
}
if (ctl.Name=="cbEnabled"+function)
{
cb.Checked = EnabledState;//修改其属性
}
}
}
}
}
}
}
}
private void defaultCheckBox()
{
foreach (Control ctl in this.Controls) //遍历窗体上的所有控件
{
if (ctl is GroupBox)//如果这个控件是groupbox
{
if (ctl.HasChildren) //如果这个控件包含子控件
{
foreach (Control cb in ctl.Controls)//遍历这个控件---groupbox
{
if (cb is CheckBox) //如果子控件是checkbox
{
CheckBox ckb = (CheckBox)cb;//将子控件转换为checkbox;类型
ckb.Checked = false;//修改checkbox的属性
//ckb.Enabled = false;
}
}
}
}
}
}
private void label3_Click(object sender, EventArgs e)
{
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
string cbbValue = cbbUID.SelectedItem.ToString();//取得当前下拉框的值,以决定更新哪条记录
for (int i = 0; i < ds_checkbox.Tables[0].Rows.Count; i++)//循环
{
string function = ds_checkbox.Tables[0].Rows[i]["Functions"].ToString();
foreach (Control ctl in this.Controls)
{
if (ctl is GroupBox)
{
if (ctl.HasChildren)
{
foreach (Control ckb in ctl.Controls)
{
if (ckb.Text == function)//这里将会找到FunctionList表中所有的checkbox,
{
if (((CheckBox)ckb).Checked)
{
ds_checkbox.Tables[0].Rows[i]["State"] = true;
//执行删除操作
//string key = ds_checkbox.Tables[0].Rows[i]["UF_No"].ToString();//取得主键
//appCode.DALL.DeleteFunction("UsersFunction", "UF_No", key);
//appCode.others.showMessage_Successfully_saved();
}
else
{
ds_checkbox.Tables[0].Rows[i]["State"] = false;
}
}
}
}
}
}
}
ds_checkbox.Tables[0].TableName = "UsersFunction";
appCode.others.UpdateByDataSet(ds_checkbox, "UsersFunction", appCode.others.connString());
appCode.others.showMessage_Successfully_saved();
}
}
}
动态创建CHECKBOX,定位显示,遍历写入数据保存数据
最新推荐文章于 2022-09-28 11:58:22 发布