一、checkedListBoxControl的使用全选
private void InitDate() { CheckedListBoxItem[] itemArr = { new CheckedListBoxItem("全选",false), new CheckedListBoxItem("星期一", false), new CheckedListBoxItem("星期二", false), new CheckedListBoxItem("星期三", false), new CheckedListBoxItem("星期四", false), new CheckedListBoxItem("星期五", true), new CheckedListBoxItem("星期六", true), new CheckedListBoxItem("星期日", false) }; checkedListBoxControl1.Items.AddRange(itemArr); checkedListBoxControl1.CheckOnClick = true;//控件点击一次就可以选中复选框 checkedListBoxControl1.ItemCheck += CheckedListBoxControl1_ItemCheck; } /// <summary> /// 监听所有ItemCheck选中状态事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void CheckedListBoxControl1_ItemCheck(object sender, DevExpress.XtraEditors.Controls.ItemCheckEventArgs e) { //MessageBox.Show(checkedListBoxControl1.GetItemText(e.Index)); //throw new NotImplementedException(); if (checkedListBoxControl1.GetItemText(e.Index) == "全选") { if (checkedListBoxControl1.GetItemChecked(0)) { //设置索引为index的项为选中状态 for (int i = 1; i < checkedListBoxControl1.Items.Count; i++) { checkedListBoxControl1.SetItemChecked(i, true); } return; } else { //设置索引为index的项为选中状态 for (int i = 1; i < checkedListBoxControl1.Items.Count; i++) { checkedListBoxControl1.SetItemChecked(i, false); } return;//return 解决了 CheckOnClick 检测单击或者双击延迟的问题 } } else { MessageBox.Show("osa"); } }
using DevExpress.XtraEditors; using DevExpress.XtraEditors.Controls; 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; using YKERP.BaseForms; using YKERP.Business; using YKERP.Common; using YKERP.Tools; namespace YKERP.SCM._ReplenishmentMgt.PurchaseOrders { public partial class ReplenishmentCycleAddOrEdit : YKBaseForm { private string series = null; //定义委托 public delegate void FuncRefresh(); //定义事件 public event FuncRefresh myRefresh; public ReplenishmentCycleAddOrEdit(Dictionary<string, string> Par) { InitializeComponent(); InitData(); if (Par != null) { this.series = Par["series"]; Select_Store.ReadOnly = true; Select_Store.EditValue = Par["sub_unit_num_id"]; Select_Status.EditValue = Par["status_num_id"]; Text_Delivery.Text = Par["delivery_day"]; char[] chs = Par["replenish_weekly_day_flag"].ToCharArray();//分成一个一个的数字 List<string> scoreList = new List<string>();//创建了一个空列表 foreach (var stu in chs) { switch (stu) { case '1': scoreList.Add("星期一"); break; case '2': scoreList.Add("星期二"); break; case '3': scoreList.Add("星期三"); break; case '4': scoreList.Add("星期四"); break; case '5': scoreList.Add("星期五"); break; case '6': scoreList.Add("星期六"); break; case '7': scoreList.Add("星期日"); break; default: break; } } System.String[] str = scoreList.ToArray(); for (int i = 1; i < checkedListBoxControl1.Items.Count; i++) { foreach (string _statisticalTime in str) { if (checkedListBoxControl1.GetItemValue(i).ToString() == _statisticalTime) { checkedListBoxControl1.SetItemChecked(i, true); } } } //CheckedComboBoxEdit 类型的控件 /* if (str != null) //修改默认选中 foreach (CheckedListBoxItem item in Text_SupplierReplenishment.Properties.Items) { foreach (string _statisticalTime in str) { if (item.Value.ToString() == _statisticalTime) { item.CheckState = CheckState.Checked; } } }*/ } } private void InitData() { Select_Store.BindData(CommonDicType.门店); var status_type = new List<object>() { new { id = "1", name = "未生效" }, new { id = "2", name = "生效" }, }; Select_Status.BindDataTable(YKTool.listToDataTable(status_type)); Select_Status.EditValue = 2; //选择第一项 //自定义数组 //string[] strs = new string[] { "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日" }; //添加项 // Text_SupplierReplenishment.Properties.Items.AddRange(strs); InitDate(); } private void InitDate() { CheckedListBoxItem[] itemArr = { new CheckedListBoxItem("全选",false), new CheckedListBoxItem("星期一", false), new CheckedListBoxItem("星期二", false), new CheckedListBoxItem("星期三", false), new CheckedListBoxItem("星期四", false), new CheckedListBoxItem("星期五", true), new CheckedListBoxItem("星期六", true), new CheckedListBoxItem("星期日", false) }; checkedListBoxControl1.Items.AddRange(itemArr); checkedListBoxControl1.CheckOnClick = true;//控件点击一次就可以选中复选框 checkedListBoxControl1.ItemCheck += CheckedListBoxControl1_ItemCheck; checkedListBoxControl1.SelectedValueChanged += CheckedListBoxControl1_SelectedValueChanged; ; } private void CheckedListBoxControl1_ItemCheck(object sender, DevExpress.XtraEditors.Controls.ItemCheckEventArgs e) { //MessageBox.Show(checkedListBoxControl1.GetItemText(e.Index)); //throw new NotImplementedException(); if (checkedListBoxControl1.GetItemText(e.Index) == "全选") { if (checkedListBoxControl1.GetItemChecked(0)) { //设置索引为index的项为选中状态 for (int i = 1; i < checkedListBoxControl1.Items.Count; i++) { if (checkedListBoxControl1.SelectedIndex != i) checkedListBoxControl1.SetItemChecked(i, true); } return; } else { //设置索引为index的项为选中状态 for (int i = 1; i < checkedListBoxControl1.Items.Count; i++) { if (checkedListBoxControl1.SelectedIndex != i) checkedListBoxControl1.SetItemChecked(i, false); } return;//return 解决了 CheckOnClick 检测单击或者双击延迟的问题 } } } private void CheckedListBoxControl1_SelectedValueChanged(object sender, EventArgs e) { List<object> objList = checkedListBoxControl1.Items.GetCheckedValues(); //解决失灵的 if (checkedListBoxControl1.GetItemChecked(checkedListBoxControl1.SelectedIndex)) { checkedListBoxControl1.SetItemCheckState(checkedListBoxControl1.SelectedIndex, CheckState.Checked); } else checkedListBoxControl1.SetItemCheckState(checkedListBoxControl1.SelectedIndex, CheckState.Unchecked); if (objList.Count() == (checkedListBoxControl1.Items.Count - 2)) { checkedListBoxControl1.SetItemCheckState(0, CheckState.Checked); //checkedListBoxControl1.SetItemCheckState(checkedListBoxControl1.SelectedIndex, CheckState.Unchecked); } if (objList.Count() == 2) { checkedListBoxControl1.SetItemCheckState(0, CheckState.Unchecked); //checkedListBoxControl1.SetItemCheckState(checkedListBoxControl1.SelectedIndex, CheckState.Unchecked); } } private void btn_AddOrEdit_Click(object sender, EventArgs e) { if (string.IsNullOrWhiteSpace(Select_Store.Text)) { MessageHelper.InfoMsg("请选择门店"); return; } //MessageBox.Show(Text_SupplierReplenishment.EditValue.ToString()); Dictionary<string, string> data = new Dictionary<string, string>(); var _Store = Select_Store.EditValue?.ToString()?.ToInt() ?? 0; if (_Store > 0) //门店 data.Add("sub_unit_num_id", _Store.ToString()); data.Add("delivery_day", Text_Delivery.Text); var _Status = Select_Status.EditValue?.ToString()?.ToInt() ?? 0; if (_Status > 0) //状态 data.Add("status_num_id", _Status.ToString()); else { MessageHelper.InfoMsg("状态不能为空"); return; } if (this.series != null) data.Add("series", this.series); // //List<object> List = Text_SupplierReplenishment.Properties.Items.GetCheckedValues(); List<Object> list = new List<object> { }; string a = null; for (int i = 0; i < checkedListBoxControl1.Items.Count; i++) { if (checkedListBoxControl1.GetItemChecked(i)) { a = checkedListBoxControl1.GetItemValue(i).ToString(); list.Add(a); } } char[] charnumbers = new char[7] { '0', '0', '0', '0', '0', '0', '0' };//定长 foreach (var stu in list) { switch (stu) { case "星期一": charnumbers[0] = '1'; break; case "星期二": charnumbers[1] = '2'; break; case "星期三": charnumbers[2] = '3'; break; case "星期四": charnumbers[3] = '4'; break; case "星期五": charnumbers[4] = '5'; break; case "星期六": charnumbers[5] = '6'; break; case "星期日": charnumbers[6] = '7'; break; default: break; } } //MessageBox.Show(res + "--" + res.Count()); data.Add("replenish_weekly_day_flag", string.Join("", charnumbers)); string result = UnitOfWork<DAL>.Resolve.ReplenishmentCycleAddOrEdit(data, out string errors); if (errors != null) { MessageHelper.InfoMsg(errors); return; } if (result != "" || result != null) { if (this.series != null) MessageHelper.InfoMsg("编辑成功"); else MessageHelper.InfoMsg("添加成功"); this.Close(); myRefresh(); } else { MessageHelper.InfoMsg(errors.ToString()); } } } }
原本的
using DevExpress.XtraEditors; using DevExpress.XtraEditors.Controls; 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; using YKERP.BaseForms; using YKERP.Business; using YKERP.Common; using YKERP.Tools; namespace YKERP.SCM._ReplenishmentMgt.PurchaseOrders { public partial class ReplenishmentCycleAddOrEdit : YKBaseForm { private string series = null; //定义委托 public delegate void FuncRefresh(); //定义事件 public event FuncRefresh myRefresh; public ReplenishmentCycleAddOrEdit(Dictionary<string, string> Par) { InitializeComponent(); InitData(); if (Par != null) { this.series = Par["series"]; Select_Store.ReadOnly = true; Select_Store.EditValue = Par["sub_unit_num_id"]; Select_Status.EditValue = Par["status_num_id"]; Text_Delivery.Text = Par["delivery_day"]; char[] chs = Par["replenish_weekly_day_flag"].ToCharArray();//分成一个一个的数字 List<string> scoreList = new List<string>();//创建了一个空列表 foreach (var stu in chs) { switch (stu) { case '1': scoreList.Add("星期一"); break; case '2': scoreList.Add("星期二"); break; case '3': scoreList.Add("星期三"); break; case '4': scoreList.Add("星期四"); break; case '5': scoreList.Add("星期五"); break; case '6': scoreList.Add("星期六"); break; case '7': scoreList.Add("星期日"); break; default: break; } } System.String[] str = scoreList.ToArray(); for (int i = 1; i < checkedListBoxControl1.Items.Count; i++) { foreach (string _statisticalTime in str) { if (checkedListBoxControl1.GetItemValue(i).ToString() == _statisticalTime) { checkedListBoxControl1.SetItemChecked(i, true); } } } //CheckedComboBoxEdit 类型的控件 /* if (str != null) //修改默认选中 foreach (CheckedListBoxItem item in Text_SupplierReplenishment.Properties.Items) { foreach (string _statisticalTime in str) { if (item.Value.ToString() == _statisticalTime) { item.CheckState = CheckState.Checked; } } }*/ } } private void InitData() { Select_Store.BindData(CommonDicType.门店); var status_type = new List<object>() { new { id = "1", name = "未生效" }, new { id = "2", name = "生效" }, }; Select_Status.BindDataTable(YKTool.listToDataTable(status_type)); Select_Status.EditValue = 2; //选择第一项 //自定义数组 //string[] strs = new string[] { "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日" }; //添加项 // Text_SupplierReplenishment.Properties.Items.AddRange(strs); InitDate(); } private void InitDate() { CheckedListBoxItem[] itemArr = { new CheckedListBoxItem("全选",false), new CheckedListBoxItem("星期一", false), new CheckedListBoxItem("星期二", false), new CheckedListBoxItem("星期三", false), new CheckedListBoxItem("星期四", false), new CheckedListBoxItem("星期五", true), new CheckedListBoxItem("星期六", true), new CheckedListBoxItem("星期日", false) }; checkedListBoxControl1.Items.AddRange(itemArr); checkedListBoxControl1.CheckOnClick = true;//控件点击一次就可以选中复选框 checkedListBoxControl1.ItemChecking += CheckedListBoxControl1_ItemChecking; ; } private void CheckedListBoxControl1_ItemChecking(object sender, ItemCheckingEventArgs e) { //MessageBox.Show(checkedListBoxControl1.GetItemText(e.Index)); //throw new NotImplementedException(); if (checkedListBoxControl1.GetItemText(e.Index) == "全选") { if (checkedListBoxControl1.GetItemChecked(0)) { //设置索引为index的项为选中状态 for (int i = 1; i < checkedListBoxControl1.Items.Count; i++) { checkedListBoxControl1.SetItemChecked(i, false); } return; } else { //设置索引为index的项为选中状态 for (int i = 1; i < checkedListBoxControl1.Items.Count; i++) { checkedListBoxControl1.SetItemChecked(i, true); } return;//return 解决了 CheckOnClick 检测单击或者双击延迟的问题 } } if (checkedListBoxControl1.GetItemText(e.Index) != "全选") { if (checkedListBoxControl1.SelectedItems.Count() == checkedListBoxControl1.Items.Count) { checkedListBoxControl1.SetItemChecked(0, true); } } } /// <summary> /// 监听所有ItemCheck选中状态事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void CheckedListBoxControl1_ItemCheck(object sender, DevExpress.XtraEditors.Controls.ItemCheckEventArgs e) { //MessageBox.Show(checkedListBoxControl1.GetItemText(e.Index)); //throw new NotImplementedException(); if (checkedListBoxControl1.GetItemText(e.Index) == "全选") { if (checkedListBoxControl1.GetItemChecked(0)) { //设置索引为index的项为选中状态 for (int i = 1; i < checkedListBoxControl1.Items.Count; i++) { checkedListBoxControl1.SetItemChecked(i, true); } return; } else { //设置索引为index的项为选中状态 for (int i = 1; i < checkedListBoxControl1.Items.Count; i++) { checkedListBoxControl1.SetItemChecked(i, false); } return;//return 解决了 CheckOnClick 检测单击或者双击延迟的问题 } } else { MessageBox.Show("osa"); } } private void btn_AddOrEdit_Click(object sender, EventArgs e) { if (string.IsNullOrWhiteSpace(Select_Store.Text)) { MessageHelper.InfoMsg("请选择门店"); return; } //MessageBox.Show(Text_SupplierReplenishment.EditValue.ToString()); Dictionary<string, string> data = new Dictionary<string, string>(); var _Store = Select_Store.EditValue?.ToString()?.ToInt() ?? 0; if (_Store > 0) //门店 data.Add("sub_unit_num_id", _Store.ToString()); data.Add("delivery_day", Text_Delivery.Text); var _Status = Select_Status.EditValue?.ToString()?.ToInt() ?? 0; if (_Status > 0) //状态 data.Add("status_num_id", _Status.ToString()); else { MessageHelper.InfoMsg("状态不能为空"); return; } if (this.series != null) data.Add("series", this.series); // //List<object> List = Text_SupplierReplenishment.Properties.Items.GetCheckedValues(); List<Object> list = new List<object> { }; string a=null; for (int i = 0; i < checkedListBoxControl1.Items.Count; i++) { if (checkedListBoxControl1.GetItemChecked(i)) { a = checkedListBoxControl1.GetItemValue(i).ToString(); list.Add(a); } } char[] charnumbers = new char[7] { '0', '0', '0', '0', '0', '0', '0' };//定长 foreach (var stu in list) { switch (stu) { case "星期一": charnumbers[0] = '1'; break; case "星期二": charnumbers[1] = '2'; break; case "星期三": charnumbers[2] = '3'; break; case "星期四": charnumbers[3] = '4'; break; case "星期五": charnumbers[4] = '5'; break; case "星期六": charnumbers[5] = '6'; break; case "星期日": charnumbers[6] = '7'; break; default: break; } } //MessageBox.Show(res + "--" + res.Count()); data.Add("replenish_weekly_day_flag", string.Join("", charnumbers)); string result = UnitOfWork<DAL>.Resolve.ReplenishmentCycleAddOrEdit(data, out string errors); if (errors!=null) { MessageHelper.InfoMsg(errors); return; } if (result != "" || result != null) { if (this.series != null) MessageHelper.InfoMsg("编辑成功"); else MessageHelper.InfoMsg("添加成功"); this.Close(); myRefresh(); } else { MessageHelper.InfoMsg(errors.ToString()); } } } }