BlurSearch.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. value = dt.Select("rownum=" + (ListBox.SelectedIndex + 1))[0][1].ToString();
  147. EnterTextBox.Text = ListBox.SelectedItem.ToString();
  148. ListBox.Visible = false;
  149. Height = EnterTextBox.Height;
  150. }
  151. private void EnterTextBox_KeyDown(object sender, KeyEventArgs e)
  152. {
  153. if (ListBox.Items.Count > 0)
  154. {
  155. if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Up)
  156. {
  157. switch (e.KeyCode)
  158. {
  159. case Keys.Down:
  160. if (ListBox.Items.Count > ItemSelectIndex + 1)
  161. ItemSelectIndex = ItemSelectIndex + 1;
  162. break;
  163. case Keys.Up:
  164. if (ItemSelectIndex - 1 >= 0)
  165. ItemSelectIndex = ItemSelectIndex - 1;
  166. break;
  167. default:
  168. break;
  169. }
  170. ListBox.SelectedIndex = ItemSelectIndex;
  171. }
  172. if (e.KeyCode == Keys.Enter)
  173. {
  174. try
  175. {
  176. EnterTextBox.Text = ListBox.SelectedItem.ToString();
  177. ListBox.Visible = false;
  178. Height = EnterTextBox.Height;
  179. }
  180. catch (Exception)
  181. {
  182. }
  183. }
  184. }
  185. }
  186. private void EnterTextBox_Enter(object sender, EventArgs e)
  187. {
  188. string con = Field1 + " like '%" + EnterTextBox.Text + "%' ";
  189. if (condition != "" && condition != null)
  190. {
  191. con = Field1 + " like '%" + EnterTextBox.Text + "%' and " + condition;
  192. }
  193. dt = dh.getFieldsDatasByCondition(TableName1, new string[] { Field1, valueField, "rownum" }, con.ToUpper());
  194. if (dt.Rows.Count > 0)
  195. {
  196. ListBox.Items.Clear();
  197. for (int i = 0; i < dt.Rows.Count; i++)
  198. {
  199. ListBox.Items.Add(dt.Rows[i][Field1]);
  200. }
  201. //每次数据查询之后将索引重置
  202. ItemSelectIndex = 0;
  203. ListBox.Height = ListBox.ItemHeight * 10;
  204. ListBox.Visible = true;
  205. Height = ListBox.Height + EnterTextBox.Height;
  206. }
  207. }
  208. private void EnterTextBox_Leave(object sender, EventArgs e)
  209. {
  210. //ListBox.Visible = false;
  211. }
  212. private void BlurSearch_Leave(object sender, EventArgs e)
  213. {
  214. ListBox.Height = 0;
  215. ListBox.Visible = false;
  216. Height = ListBox.Height + EnterTextBox.Height;
  217. }
  218. }
  219. }