MaCodeSearchTextBox.cs 8.6 KB

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