BlurSearch.cs 6.8 KB

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