MaCodeSearchTextBox.cs 9.1 KB

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