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;
///
/// 控件数据改变
///
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 GetValue()
{
List DataItems = new List();
foreach (DataRowView Item in ChkList.CheckedItems)
{
DataItems.Add(Item[ChkList.ValueMember].ToString());
}
return DataItems;
}
public void SetValue(List 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);
}
}
}
}
///
/// 刷新数据
///
public void RefreshData(DataTable dt, string KeyField, string DisplayField)
{
List Itemlist = GetValue();
ChkList.DataSource = dt;
ChkList.DisplayMember = DisplayField;
ChkList.ValueMember = KeyField;
SetValue(Itemlist);
}
///
/// 是否控件输入值是否为空
///
/// true,空值;false,已经输入值
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);
}
}
}