SearchTextBox.cs 8.5 KB

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