SearchTextBox.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. using System;
  2. using System.Data;
  3. using System.Windows.Forms;
  4. namespace FileWatcher
  5. {
  6. public partial class SearchTextBox : UserControl
  7. {
  8. #region 构造函数
  9. public SearchTextBox()
  10. {
  11. InitializeComponent();
  12. }
  13. #endregion
  14. #region 变量
  15. /// <summary>
  16. /// 是否通过Leave事件找到数据
  17. /// </summary>
  18. public bool LeaveFindData;
  19. /// <summary>
  20. /// DBFind窗体弹出的标题
  21. /// </summary>
  22. private string DBTitle1;
  23. /// <summary>
  24. /// 输入框是否可编辑
  25. /// </summary>
  26. private bool TextBoxEnable1;
  27. /// <summary>
  28. /// 业务逻辑的标识符
  29. /// </summary>
  30. private string caller;
  31. /// <summary>
  32. /// 发起DbFind的窗体
  33. /// </summary>
  34. private string formName;
  35. /// <summary>
  36. /// 需要赋值的字段
  37. /// </summary>
  38. private string[] setValueField;
  39. /// <summary>
  40. /// 初始的筛选条件
  41. /// </summary>
  42. private string condition;
  43. /// <summary>
  44. /// 权限标识
  45. /// </summary>
  46. private string Power1;
  47. /// <summary>
  48. /// 查询的表名
  49. /// </summary>
  50. private string tableName;
  51. /// <summary>
  52. /// 查询的字段
  53. /// </summary>
  54. private string selectField;
  55. DataTable dt = new DataTable();
  56. DbFind db;
  57. private DataTable returnData;
  58. public override string Text
  59. {
  60. get
  61. {
  62. return TextBox.Text;
  63. }
  64. set
  65. {
  66. TextBox.Text = value;
  67. }
  68. }
  69. string AllPower1;
  70. public string AllPower
  71. {
  72. get
  73. {
  74. return AllPower1;
  75. }
  76. set
  77. {
  78. AllPower1 = value;
  79. }
  80. }
  81. public string Caller
  82. {
  83. get
  84. {
  85. return caller;
  86. }
  87. set
  88. {
  89. caller = value;
  90. }
  91. }
  92. public string FormName
  93. {
  94. get
  95. {
  96. return formName;
  97. }
  98. set
  99. {
  100. formName = value;
  101. }
  102. }
  103. public string[] SetValueField
  104. {
  105. get
  106. {
  107. return setValueField;
  108. }
  109. set
  110. {
  111. setValueField = value;
  112. }
  113. }
  114. public string Condition
  115. {
  116. get
  117. {
  118. return condition;
  119. }
  120. set
  121. {
  122. condition = value;
  123. }
  124. }
  125. public string TableName
  126. {
  127. get
  128. {
  129. return tableName;
  130. }
  131. set
  132. {
  133. tableName = value;
  134. }
  135. }
  136. public string SelectField
  137. {
  138. get
  139. {
  140. return selectField;
  141. }
  142. set
  143. {
  144. selectField = value;
  145. }
  146. }
  147. public string Power
  148. {
  149. get
  150. {
  151. return Power1;
  152. }
  153. set
  154. {
  155. Power1 = value;
  156. }
  157. }
  158. public string DBTitle
  159. {
  160. get
  161. {
  162. return DBTitle1;
  163. }
  164. set
  165. {
  166. DBTitle1 = value;
  167. }
  168. }
  169. public bool TextBoxEnable
  170. {
  171. get
  172. {
  173. return TextBoxEnable1;
  174. }
  175. set
  176. {
  177. TextBoxEnable1 = value;
  178. }
  179. }
  180. public DataTable ReturnData
  181. {
  182. get
  183. {
  184. return returnData;
  185. }
  186. set
  187. {
  188. returnData = value;
  189. }
  190. }
  191. #endregion
  192. #region 自定义事件
  193. public delegate void OnTextChange(object sender, EventArgs e);
  194. //定义事件
  195. public event OnTextChange UserControlTextChanged;
  196. //定义委托
  197. public delegate void Icon_Click(object sender, EventArgs e);
  198. /// <summary>
  199. /// 图标点击事件
  200. /// </summary>
  201. public event Icon_Click SearchIconClick;
  202. public delegate void DBSourceChange(object sender, EventArgs e);
  203. public event DBSourceChange DbChange;
  204. public delegate void OnTextKeyDown(object sender, KeyEventArgs e);
  205. public event OnTextKeyDown TextKeyDown;
  206. #endregion
  207. //定义委托
  208. private void TextBox_TextChanged(object sender, EventArgs e)
  209. {
  210. UserControlTextChanged?.Invoke(sender, new EventArgs());
  211. }
  212. //Key先发起失去焦点事件,在执行用户自定义的事件
  213. private void TextBox_KeyDown(object sender, KeyEventArgs e)
  214. {
  215. if (e.KeyCode == Keys.Enter)
  216. {
  217. TextBox_Leave(sender, e);
  218. TextKeyDown?.Invoke(sender, new KeyEventArgs(e.KeyCode));
  219. }
  220. }
  221. private void Search_Icon_Click(object sender, EventArgs e)
  222. {
  223. SearchIconClick?.Invoke(sender, new EventArgs());
  224. db = new DbFind(Name, tableName, selectField, setValueField, caller, formName, condition);
  225. db.Text = DBTitle1;
  226. if (db.IsAbleDbFind1)
  227. {
  228. db.ShowDialog();
  229. GetData();
  230. }
  231. else
  232. {
  233. MessageBox.Show("该字段未配置DbFind", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  234. }
  235. }
  236. /// <summary>
  237. /// 触发给界面的
  238. /// </summary>
  239. /// <param name="sender"></param>
  240. /// <param name="e"></param>
  241. public void ReturnDataToForm()
  242. {
  243. returnData = dt;
  244. DbChange?.Invoke(new object(), new EventArgs());
  245. }
  246. private void TextBox_Enter(object sender, EventArgs e)
  247. {
  248. TextBox.BackColor = System.Drawing.Color.GreenYellow;
  249. }
  250. /// <summary>
  251. /// 重新刷新数据
  252. /// </summary>
  253. /// <param name="sender"></param>
  254. /// <param name="e"></param>
  255. public void RefreshDB(object sender, EventArgs e)
  256. {
  257. TextBox_Leave(sender, e);
  258. GetData();
  259. }
  260. public void TextBox_Leave(object sender, EventArgs e)
  261. {
  262. TextBox.BackColor = System.Drawing.Color.White;
  263. GetData();
  264. }
  265. private void SearchTextBox_Load(object sender, EventArgs e)
  266. {
  267. if (!TextBoxEnable1)
  268. TextBox.BackColor = System.Drawing.Color.White;
  269. TextBox.Enabled = TextBoxEnable1;
  270. Search_Icon.Power = this.Power;
  271. }
  272. private void SearchTextBox_SizeChanged(object sender, EventArgs e)
  273. {
  274. TextBox.Width = this.Width - Search_Icon.Width - 3;
  275. }
  276. public void GetData()
  277. {
  278. DataHelper dh = SystemInf.dh;
  279. string sql = "";
  280. //将查询到的结果返回界面
  281. if (TextBox.Text != "")
  282. {
  283. sql = "select " + BaseUtil.AddField(setValueField) + " from " + tableName + " where " + Name + "='" + TextBox.Text + "'";
  284. }
  285. else if (db != null)
  286. {
  287. sql = "select " + BaseUtil.AddField(setValueField) + " from " + tableName + " where " + Name + "='" + db.TextBoxValue1 + "'";
  288. }
  289. if (condition != null)
  290. {
  291. sql += " and " + condition;
  292. }
  293. try
  294. {
  295. dt = (DataTable)dh.ExecuteSql(sql, "select");
  296. if (dt.Rows.Count > 0)
  297. ReturnDataToForm();
  298. }
  299. catch (Exception e)
  300. {
  301. }
  302. }
  303. }
  304. }