BlurSearch.cs 6.8 KB

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