MaCodeSearchTextBox.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. }
  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. /// <summary>
  61. /// 设置绑定的CheckBox
  62. /// </summary>
  63. LockCheckBox LockCheckBox;
  64. public delegate void DBSourceChange(object sender, EventArgs e);
  65. public event DBSourceChange DbChange;
  66. DataTable dt = new DataTable();
  67. DbFind db;
  68. DataTable ReturnData1;
  69. public override string Text
  70. {
  71. get
  72. {
  73. return TextBox.Text.ToUpper();
  74. }
  75. set
  76. {
  77. TextBox.Text = value;
  78. }
  79. }
  80. string AllPower1;
  81. public string AllPower
  82. {
  83. get
  84. {
  85. return AllPower1;
  86. }
  87. set
  88. {
  89. AllPower1 = value;
  90. }
  91. }
  92. public string Caller
  93. {
  94. get
  95. {
  96. return caller;
  97. }
  98. set
  99. {
  100. caller = value;
  101. }
  102. }
  103. public string FormName
  104. {
  105. get
  106. {
  107. return formName;
  108. }
  109. set
  110. {
  111. formName = value;
  112. }
  113. }
  114. public string[] SetValueField
  115. {
  116. get
  117. {
  118. return setValueField;
  119. }
  120. set
  121. {
  122. setValueField = value;
  123. }
  124. }
  125. public string Condition
  126. {
  127. get
  128. {
  129. return condition;
  130. }
  131. set
  132. {
  133. condition = value;
  134. }
  135. }
  136. public string TableName
  137. {
  138. get
  139. {
  140. return tableName;
  141. }
  142. set
  143. {
  144. tableName = value;
  145. }
  146. }
  147. public string SelectField
  148. {
  149. get
  150. {
  151. return selectField;
  152. }
  153. set
  154. {
  155. selectField = value;
  156. }
  157. }
  158. public string Power
  159. {
  160. get
  161. {
  162. return Power1;
  163. }
  164. set
  165. {
  166. Power1 = value;
  167. }
  168. }
  169. public string DBTitle
  170. {
  171. get
  172. {
  173. return DBTitle1;
  174. }
  175. set
  176. {
  177. DBTitle1 = value;
  178. }
  179. }
  180. public bool TextBoxEnable
  181. {
  182. get
  183. {
  184. return TextBoxEnable1;
  185. }
  186. set
  187. {
  188. TextBoxEnable1 = value;
  189. }
  190. }
  191. public DataTable ReturnData
  192. {
  193. get
  194. {
  195. return ReturnData1;
  196. }
  197. set
  198. {
  199. ReturnData1 = value;
  200. }
  201. }
  202. #endregion
  203. #region 自定义事件
  204. public delegate void OnTextChange(object sender, EventArgs e);
  205. //定义事件
  206. public event OnTextChange UserControlTextChanged;
  207. //定义委托
  208. public delegate void Icon_Click(object sender, EventArgs e);
  209. /// <summary>
  210. /// 图标点击事件
  211. /// </summary>
  212. public event Icon_Click SearchIconClick;
  213. #endregion
  214. //定义委托
  215. private void TextBox_TextChanged(object sender, EventArgs e)
  216. {
  217. UserControlTextChanged?.Invoke(sender, new EventArgs());
  218. }
  219. //Key先发起失去焦点事件,在执行用户自定义的事件
  220. private void TextBox_KeyDown(object sender, KeyEventArgs e)
  221. {
  222. if (e.KeyCode == Keys.Enter)
  223. GetData(true);
  224. }
  225. public void SetLockCheckBox(LockCheckBox ctl)
  226. {
  227. LockCheckBox = ctl;
  228. }
  229. public void GetData(bool LeaveOrEnter)
  230. {
  231. DataHelper dh = SystemInf.dh;
  232. string sql = "";
  233. //将查询到的结果返回界面
  234. if (TextBox.Text != "")
  235. {
  236. sql = "select " + BaseUtil.AddField(setValueField) + " from " + tableName + " where " + Name + "='" + TextBox.Text.ToUpper() + "'";
  237. }
  238. else if (db != null)
  239. {
  240. sql = "select " + BaseUtil.AddField(setValueField) + " from " + tableName + " where " + Name + "='" + db.TextBoxValue1 + "'";
  241. }
  242. if (condition != null)
  243. {
  244. sql += " and " + condition;
  245. }
  246. try
  247. {
  248. dt = (DataTable)dh.ExecuteSql(sql, "select");
  249. if (dt.Rows.Count > 0)
  250. ReturnDataToFrom();
  251. }
  252. catch (Exception e)
  253. {
  254. LogManager.DoLog(e.Message);
  255. }
  256. if (LeaveOrEnter || TextBox.Text != "")
  257. LockCheckBox.Checked = true;
  258. }
  259. private void fillControl(int i, Control ct)
  260. {
  261. for (int j = 0; j < setValueField.Length; j++)
  262. {
  263. if (ct.Controls.Count > 0 && ct.Name.ToString() != setValueField[j])
  264. {
  265. Control.ControlCollection controls = ct.Controls;
  266. for (int k = 0; k < ct.Controls.Count; k++)
  267. {
  268. fillControl(i, controls[k]);
  269. }
  270. }
  271. else
  272. {
  273. 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])
  274. ct.Text = dt.Rows[0][dt.Columns[i].ColumnName].ToString();
  275. }
  276. }
  277. }
  278. private void Search_Icon_Click(object sender, EventArgs e)
  279. {
  280. SearchIconClick?.Invoke(sender, new EventArgs());
  281. db = new DbFind(Name, tableName, selectField, setValueField, caller, formName, condition);
  282. db.Text = DBTitle1;
  283. LogManager.DoLog("DbFind查询,发起窗口【" + formName + "】,查询表【" + tableName + "】,字段" + selectField + ",条件" + condition);
  284. if (db.IsAbleDbFind1)
  285. {
  286. db.ShowDialog();
  287. GetData(true);
  288. }
  289. else
  290. {
  291. MessageBox.Show("该字段未配置DbFind", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  292. }
  293. }
  294. private void ReturnDataToFrom()
  295. {
  296. ReturnData1 = dt;
  297. DbChange?.Invoke(new object(), new EventArgs());
  298. }
  299. public void TextBox_Leave(object sender, EventArgs e)
  300. {
  301. //GetData(false);
  302. }
  303. private void SearchTextBox_Load(object sender, EventArgs e)
  304. {
  305. if (!TextBoxEnable1)
  306. TextBox.BackColor = System.Drawing.Color.White;
  307. TextBox.Enabled = TextBoxEnable1;
  308. }
  309. private void SearchTextBox_SizeChanged(object sender, EventArgs e)
  310. {
  311. TextBox.Width = this.Width - Search_Icon.Width - 3;
  312. }
  313. private void MaCodeSearchTextBox_Leave(object sender, EventArgs e)
  314. {
  315. if (TextBox.Text != "")
  316. GetData(false);
  317. }
  318. }
  319. }