BlurSearch.cs 6.8 KB

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