LC_DropChkList.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. namespace WCS_Client.UC
  8. {
  9. public class LC_DropChkList : DevComponents.DotNetBar.Controls.TextBoxDropDown
  10. {
  11. public LC_DropChkList()
  12. {
  13. ChkList.CheckOnClick = true;
  14. ChkList.ItemCheck += new ItemCheckEventHandler(ChkList_ItemCheck);
  15. this.BackgroundStyle.Class = "TextBoxBorder";
  16. this.BackgroundStyle.CornerType = DevComponents.DotNetBar.eCornerType.Square;
  17. this.Style = DevComponents.DotNetBar.eDotNetBarStyle.StyleManagerControlled;
  18. this.BackColor = System.Drawing.SystemColors.Control;
  19. this.Margin = new System.Windows.Forms.Padding(0, 0, 0, 0);
  20. this.ReadOnly = true;
  21. this.ButtonCustom.Visible = false;
  22. this.ButtonCustom2.Visible = false;
  23. this.ButtonDropDown.Visible = true;
  24. this.DropDownControl = ChkList;
  25. this.ButtonDropDown.Shortcut = DevComponents.DotNetBar.eShortcut.Ins;
  26. this.ButtonDropDownClick += new System.ComponentModel.CancelEventHandler(Item_ButtonDropDownClick);
  27. this.ButtonClear.Visible = true;
  28. this.ButtonClear.Shortcut = DevComponents.DotNetBar.eShortcut.Del;
  29. this.ButtonClearClick += new System.ComponentModel.CancelEventHandler(Item_ButtonClearClick);
  30. this.PopupClose += new EventHandler(Item_PopupClose);
  31. }
  32. private CheckedListBox ChkList = new CheckedListBox();
  33. public delegate void DataChangeHandler();
  34. public DataChangeHandler DataChangeEvent;
  35. /// <summary>
  36. /// 控件数据改变
  37. /// </summary>
  38. private void Item_PopupClose(object sender, EventArgs e)
  39. {
  40. if (DataChangeEvent != null)
  41. {
  42. DataChangeEvent();
  43. }
  44. }
  45. private void Item_ButtonDropDownClick(object sender, System.ComponentModel.CancelEventArgs e)
  46. {
  47. this.ChkList.Width = this.Width - this.Margin.Left - this.Margin.Right;
  48. this.ShowDropDown();
  49. ChkList.Focus();
  50. }
  51. private void ChkList_ItemCheck(object sender, ItemCheckEventArgs e)
  52. {
  53. int count = ChkList.Items.Count;
  54. string s = "";
  55. bool ctag = true;
  56. for (int i = 0; i < count; i++)
  57. {
  58. if (i == e.Index)
  59. {
  60. if (e.NewValue == CheckState.Checked)
  61. {
  62. DataRowView drv = ChkList.Items[i] as DataRowView;
  63. if (drv != null)
  64. {
  65. if (ctag)
  66. {
  67. ctag = false;
  68. s = drv[ChkList.DisplayMember].ToString();
  69. continue;
  70. }
  71. s = s + "," + drv[ChkList.DisplayMember].ToString();
  72. }
  73. }
  74. }
  75. else
  76. {
  77. if (ChkList.GetItemCheckState(i) == CheckState.Checked)
  78. {
  79. DataRowView drv = ChkList.Items[i] as DataRowView;
  80. if (drv != null)
  81. {
  82. if (ctag)
  83. {
  84. ctag = false;
  85. s = drv[ChkList.DisplayMember].ToString();
  86. continue;
  87. }
  88. s = s + "," + drv[ChkList.DisplayMember].ToString();
  89. }
  90. }
  91. }
  92. }
  93. this.Text = s;
  94. }
  95. public List<string> GetValue()
  96. {
  97. List<string> DataItems = new List<string>();
  98. foreach (DataRowView Item in ChkList.CheckedItems)
  99. {
  100. DataItems.Add(Item[ChkList.ValueMember].ToString());
  101. }
  102. return DataItems;
  103. }
  104. public void SetValue(List<string> DataItems)
  105. {
  106. Item_ButtonClearClick(null, null);
  107. if (DataItems != null && DataItems.Count > 0)
  108. {
  109. int count = ChkList.Items.Count;
  110. for (int i = 0; i < count; i++)
  111. {
  112. DataRowView drv = ChkList.Items[i] as DataRowView;
  113. if (drv != null && DataItems.Contains(drv[ChkList.ValueMember].ToString()))
  114. {
  115. ChkList.SetItemCheckState(i, CheckState.Checked);
  116. }
  117. }
  118. }
  119. }
  120. /// <summary>
  121. /// 刷新数据
  122. /// </summary>
  123. public void RefreshData(DataTable dt, string KeyField, string DisplayField)
  124. {
  125. List<string> Itemlist = GetValue();
  126. ChkList.DataSource = dt;
  127. ChkList.DisplayMember = DisplayField;
  128. ChkList.ValueMember = KeyField;
  129. SetValue(Itemlist);
  130. }
  131. /// <summary>
  132. /// 是否控件输入值是否为空
  133. /// </summary>
  134. /// <returns>true,空值;false,已经输入值</returns>
  135. public bool IsValueNull()
  136. {
  137. return (ChkList.CheckedItems.Count == 0);
  138. }
  139. private void Item_ButtonClearClick(object sender, System.ComponentModel.CancelEventArgs e)
  140. {
  141. this.Text = "";
  142. foreach (int item in ChkList.CheckedIndices)
  143. {
  144. ChkList.SetItemCheckState(item, CheckState.Unchecked);
  145. }
  146. }
  147. public void ClearValue()
  148. {
  149. Item_ButtonClearClick(null, null);
  150. }
  151. }
  152. }