BlurSearch.cs 6.7 KB

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