123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Data;
- namespace WCS_Client.UC
- {
- public class LC_DropChkList : DevComponents.DotNetBar.Controls.TextBoxDropDown
- {
- public LC_DropChkList()
- {
- ChkList.CheckOnClick = true;
- ChkList.ItemCheck += new ItemCheckEventHandler(ChkList_ItemCheck);
- this.BackgroundStyle.Class = "TextBoxBorder";
- this.BackgroundStyle.CornerType = DevComponents.DotNetBar.eCornerType.Square;
- this.Style = DevComponents.DotNetBar.eDotNetBarStyle.StyleManagerControlled;
- this.BackColor = System.Drawing.SystemColors.Control;
- this.Margin = new System.Windows.Forms.Padding(0, 0, 0, 0);
- this.ReadOnly = true;
- this.ButtonCustom.Visible = false;
- this.ButtonCustom2.Visible = false;
- this.ButtonDropDown.Visible = true;
- this.DropDownControl = ChkList;
- this.ButtonDropDown.Shortcut = DevComponents.DotNetBar.eShortcut.Ins;
- this.ButtonDropDownClick += new System.ComponentModel.CancelEventHandler(Item_ButtonDropDownClick);
- this.ButtonClear.Visible = true;
- this.ButtonClear.Shortcut = DevComponents.DotNetBar.eShortcut.Del;
- this.ButtonClearClick += new System.ComponentModel.CancelEventHandler(Item_ButtonClearClick);
- this.PopupClose += new EventHandler(Item_PopupClose);
- }
- private CheckedListBox ChkList = new CheckedListBox();
- public delegate void DataChangeHandler();
- public DataChangeHandler DataChangeEvent;
- /// <summary>
- /// 控件数据改变
- /// </summary>
- private void Item_PopupClose(object sender, EventArgs e)
- {
- if (DataChangeEvent != null)
- {
- DataChangeEvent();
- }
- }
- private void Item_ButtonDropDownClick(object sender, System.ComponentModel.CancelEventArgs e)
- {
- this.ChkList.Width = this.Width - this.Margin.Left - this.Margin.Right;
- this.ShowDropDown();
- ChkList.Focus();
- }
- private void ChkList_ItemCheck(object sender, ItemCheckEventArgs e)
- {
- int count = ChkList.Items.Count;
- string s = "";
- bool ctag = true;
- for (int i = 0; i < count; i++)
- {
- if (i == e.Index)
- {
- if (e.NewValue == CheckState.Checked)
- {
- DataRowView drv = ChkList.Items[i] as DataRowView;
- if (drv != null)
- {
- if (ctag)
- {
- ctag = false;
- s = drv[ChkList.DisplayMember].ToString();
- continue;
- }
- s = s + "," + drv[ChkList.DisplayMember].ToString();
- }
- }
- }
- else
- {
- if (ChkList.GetItemCheckState(i) == CheckState.Checked)
- {
- DataRowView drv = ChkList.Items[i] as DataRowView;
- if (drv != null)
- {
- if (ctag)
- {
- ctag = false;
- s = drv[ChkList.DisplayMember].ToString();
- continue;
- }
- s = s + "," + drv[ChkList.DisplayMember].ToString();
- }
- }
- }
- }
- this.Text = s;
- }
- public List<string> GetValue()
- {
- List<string> DataItems = new List<string>();
- foreach (DataRowView Item in ChkList.CheckedItems)
- {
- DataItems.Add(Item[ChkList.ValueMember].ToString());
- }
- return DataItems;
- }
- public void SetValue(List<string> DataItems)
- {
- Item_ButtonClearClick(null, null);
- if (DataItems != null && DataItems.Count > 0)
- {
- int count = ChkList.Items.Count;
- for (int i = 0; i < count; i++)
- {
- DataRowView drv = ChkList.Items[i] as DataRowView;
- if (drv != null && DataItems.Contains(drv[ChkList.ValueMember].ToString()))
- {
- ChkList.SetItemCheckState(i, CheckState.Checked);
- }
- }
- }
- }
- /// <summary>
- /// 刷新数据
- /// </summary>
- public void RefreshData(DataTable dt, string KeyField, string DisplayField)
- {
- List<string> Itemlist = GetValue();
- ChkList.DataSource = dt;
- ChkList.DisplayMember = DisplayField;
- ChkList.ValueMember = KeyField;
- SetValue(Itemlist);
- }
- /// <summary>
- /// 是否控件输入值是否为空
- /// </summary>
- /// <returns>true,空值;false,已经输入值</returns>
- public bool IsValueNull()
- {
- return (ChkList.CheckedItems.Count == 0);
- }
- private void Item_ButtonClearClick(object sender, System.ComponentModel.CancelEventArgs e)
- {
- this.Text = "";
- foreach (int item in ChkList.CheckedIndices)
- {
- ChkList.SetItemCheckState(item, CheckState.Unchecked);
- }
- }
- public void ClearValue()
- {
- Item_ButtonClearClick(null, null);
- }
- }
- }
|