MaCodeSearchTextBox.cs 8.1 KB

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