BlurSearch.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using UAS_MES_NEW.DataOperate;
  10. using UAS_MES_NEW.Entity;
  11. namespace UAS_MES_NEW.CustomControl.TextBoxWithIcon
  12. {
  13. public partial class BlurSearch : UserControl
  14. {
  15. DataHelper dh;
  16. public delegate void OnTextChange(object sender, EventArgs e);
  17. public event OnTextChange UserControlTextChanged;
  18. string TableName1;
  19. string Field1;
  20. int ItemSelectIndex = 0;
  21. DataTable dt;
  22. public override string Text
  23. {
  24. get
  25. {
  26. return EnterTextBox.Text;
  27. }
  28. set
  29. {
  30. EnterTextBox.Text = value;
  31. }
  32. }
  33. public BlurSearch()
  34. {
  35. InitializeComponent();
  36. }
  37. public string TableName
  38. {
  39. get
  40. {
  41. return TableName1;
  42. }
  43. set
  44. {
  45. TableName1 = value;
  46. }
  47. }
  48. private string value;
  49. public string Field
  50. {
  51. get
  52. {
  53. return Field1;
  54. }
  55. set
  56. {
  57. Field1 = value;
  58. }
  59. }
  60. public string Condition
  61. {
  62. get
  63. {
  64. return condition;
  65. }
  66. set
  67. {
  68. condition = value;
  69. }
  70. }
  71. public string ValueField
  72. {
  73. get
  74. {
  75. return valueField;
  76. }
  77. set
  78. {
  79. valueField = value;
  80. }
  81. }
  82. public string Value
  83. {
  84. get
  85. {
  86. return value;
  87. }
  88. set
  89. {
  90. this.value = value;
  91. }
  92. }
  93. private string valueField;
  94. private string condition;
  95. private void TextBox_Leave(object sender, EventArgs e)
  96. {
  97. Height = EnterTextBox.Height;
  98. ListBox.Visible = false;
  99. }
  100. private void BlurSearch_Load(object sender, EventArgs e)
  101. {
  102. Height = EnterTextBox.Height;
  103. ListBox.Visible = false;
  104. if (dh == null)
  105. {
  106. dh = new DataHelper();
  107. }
  108. }
  109. private void EnterTextBox_TextChanged(object sender, EventArgs e)
  110. {
  111. if (TableName1 != null && EnterTextBox.Focused)
  112. {
  113. string con = Field1 + " like '%" + EnterTextBox.Text + "%' ";
  114. if (condition != "" && condition != null)
  115. {
  116. con = Field1 + " like '%" + EnterTextBox.Text + "%' and " + condition;
  117. }
  118. dt = dh.getFieldsDatasByCondition(TableName1, new string[] { Field1, valueField, "rownum" }, con.ToUpper());
  119. if (dt.Rows.Count > 0)
  120. {
  121. ListBox.Items.Clear();
  122. for (int i = 0; i < dt.Rows.Count; i++)
  123. {
  124. ListBox.Items.Add(dt.Rows[i][Field1]);
  125. }
  126. //每次数据查询之后将索引重置
  127. ItemSelectIndex = 0;
  128. ListBox.Height = ListBox.ItemHeight * 10;
  129. ListBox.Visible = true;
  130. Height = ListBox.Height + EnterTextBox.Height;
  131. }
  132. if (dt.Rows.Count == 1)
  133. {
  134. value = dt.Rows[0][valueField].ToString();
  135. }
  136. }
  137. else
  138. {
  139. ListBox.Visible = false;
  140. Height = EnterTextBox.Height;
  141. }
  142. UserControlTextChanged?.Invoke(sender, new EventArgs());
  143. }
  144. private void ListBox_Click(object sender, EventArgs e)
  145. {
  146. try
  147. {
  148. value = dt.Select("rownum=" + (ListBox.SelectedIndex + 1))[0][1].ToString();
  149. EnterTextBox.Text = ListBox.SelectedItem.ToString();
  150. ListBox.Visible = false;
  151. Height = EnterTextBox.Height;
  152. }
  153. catch (Exception)
  154. {
  155. }
  156. }
  157. private void EnterTextBox_KeyDown(object sender, KeyEventArgs e)
  158. {
  159. if (ListBox.Items.Count > 0)
  160. {
  161. if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Up)
  162. {
  163. switch (e.KeyCode)
  164. {
  165. case Keys.Down:
  166. if (ListBox.Items.Count > ItemSelectIndex + 1)
  167. ItemSelectIndex = ItemSelectIndex + 1;
  168. break;
  169. case Keys.Up:
  170. if (ItemSelectIndex - 1 >= 0)
  171. ItemSelectIndex = ItemSelectIndex - 1;
  172. break;
  173. default:
  174. break;
  175. }
  176. ListBox.SelectedIndex = ItemSelectIndex;
  177. }
  178. if (e.KeyCode == Keys.Enter)
  179. {
  180. try
  181. {
  182. EnterTextBox.Text = ListBox.SelectedItem.ToString();
  183. ListBox.Visible = false;
  184. Height = EnterTextBox.Height;
  185. }
  186. catch (Exception)
  187. {
  188. }
  189. }
  190. }
  191. }
  192. private void EnterTextBox_Enter(object sender, EventArgs e)
  193. {
  194. string con = Field1 + " like '%" + EnterTextBox.Text + "%' ";
  195. if (condition != "" && condition != null)
  196. {
  197. con = Field1 + " like '%" + EnterTextBox.Text + "%' and " + condition;
  198. }
  199. dt = dh.getFieldsDatasByCondition(TableName1, new string[] { Field1, valueField, "rownum" }, con.ToUpper());
  200. if (dt.Rows.Count > 0)
  201. {
  202. ListBox.Items.Clear();
  203. for (int i = 0; i < dt.Rows.Count; i++)
  204. {
  205. ListBox.Items.Add(dt.Rows[i][Field1]);
  206. }
  207. //每次数据查询之后将索引重置
  208. ItemSelectIndex = 0;
  209. ListBox.Height = ListBox.ItemHeight * 10;
  210. ListBox.Visible = true;
  211. Height = ListBox.Height + EnterTextBox.Height;
  212. }
  213. }
  214. private void EnterTextBox_Leave(object sender, EventArgs e)
  215. {
  216. //ListBox.Visible = false;
  217. }
  218. private void BlurSearch_Leave(object sender, EventArgs e)
  219. {
  220. ListBox.Height = 0;
  221. ListBox.Visible = false;
  222. Height = ListBox.Height + EnterTextBox.Height;
  223. }
  224. }
  225. }