SearchTextBox.cs 8.4 KB

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