BlurSearch.cs 6.6 KB

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