BlurSearch.cs 6.7 KB

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