BlurSearch.cs 7.0 KB

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