MaCodeSearchTextBox.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Windows.Forms;
  5. using UAS_MES.DataOperate;
  6. using UAS_MES.PublicMethod;
  7. namespace UAS_MES.CustomControl.TextBoxWithIcon
  8. {
  9. public partial class MaCodeSearchTextBox : UserControl
  10. {
  11. #region 构造函数
  12. public MaCodeSearchTextBox()
  13. {
  14. InitializeComponent();
  15. }
  16. #endregion
  17. #region 变量
  18. /// <summary>
  19. /// 是否通过Leave事件找到数据
  20. /// </summary>
  21. public bool LeaveFindData;
  22. /// <summary>
  23. /// DBFind窗体弹出的标题
  24. /// </summary>
  25. private string DBTitle1;
  26. /// <summary>
  27. /// 输入框是否可编辑
  28. /// </summary>
  29. private bool TextBoxEnable1;
  30. /// <summary>
  31. /// 业务逻辑的标识符
  32. /// </summary>
  33. private string caller;
  34. /// <summary>
  35. /// 发起DbFind的窗体
  36. /// </summary>
  37. private string formName;
  38. /// <summary>
  39. /// 需要赋值的字段
  40. /// </summary>
  41. private string[] setValueField;
  42. /// <summary>
  43. /// 初始的筛选条件
  44. /// </summary>
  45. private string condition;
  46. /// <summary>
  47. /// 权限标识
  48. /// </summary>
  49. private string Power1;
  50. /// <summary>
  51. /// 查询的表名
  52. /// </summary>
  53. private string tableName;
  54. /// <summary>
  55. /// 查询的字段
  56. /// </summary>
  57. private string selectField;
  58. /// <summary>
  59. /// 设置绑定的CheckBox
  60. /// </summary>
  61. CheckBox LockCheckBox;
  62. DbFind db;
  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. #endregion
  186. #region 自定义事件
  187. public delegate void OnTextChange(object sender, EventArgs e);
  188. //定义事件
  189. public event OnTextChange UserControlTextChanged;
  190. //定义委托
  191. public delegate void Icon_Click(object sender, EventArgs e);
  192. /// <summary>
  193. /// 图标点击事件
  194. /// </summary>
  195. public event Icon_Click SearchIconClick;
  196. #endregion
  197. //定义委托
  198. private void TextBox_TextChanged(object sender, EventArgs e)
  199. {
  200. try
  201. {
  202. if (db.SuccessReturnData)
  203. LockCheckBox.Checked = true;
  204. }
  205. catch (Exception) { }
  206. UserControlTextChanged?.Invoke(sender, new EventArgs());
  207. }
  208. //Key先发起失去焦点事件,在执行用户自定义的事件
  209. private void TextBox_KeyDown(object sender, KeyEventArgs e)
  210. {
  211. if (e.KeyCode == Keys.Enter) {
  212. GetData();
  213. }
  214. }
  215. public void SetLockCheckBox(CheckBox ctl)
  216. {
  217. LockCheckBox = ctl;
  218. }
  219. private void GetData()
  220. {
  221. if (TextBox.Text != "")
  222. {
  223. DataHelper dh = new DataHelper();
  224. List<string> fields = new List<string>();
  225. //获取查询的字段
  226. string[] field = selectField.Replace(",", "#").Trim().Split('#');
  227. for (int i = 0; i < field.Length; i++)
  228. {
  229. if (i % 2 == 0)
  230. fields.Add(field[i]);
  231. }
  232. //将查询到的结果返回界面
  233. string sql = "select " + BaseUtil.AddField(fields.ToArray()) + " from " + tableName + " where " + Tag + "='" + TextBox.Text.ToUpper() + "'";
  234. if (condition != null)
  235. {
  236. sql += " and " + condition;
  237. }
  238. DataTable dt = (DataTable)dh.ExecuteSql(sql, "select");
  239. FormCollection fmCollection = Application.OpenForms;
  240. if (dt.Rows.Count > 0)
  241. {
  242. for (int i = 0; i < dt.Columns.Count; i++)
  243. {
  244. for (int j = 0; j < SetValueField.Length; j++)
  245. {
  246. //利用字段的Tag属性来和列名进行比对
  247. object tag = fmCollection[FormName].Controls[SetValueField[j]].Tag;
  248. if (SetValueField[j] == dt.Columns[i].ColumnName.ToLower() || (tag != null && tag == dt.Columns[i].ColumnName.ToLower()))
  249. {
  250. fmCollection[FormName].Controls[SetValueField[j]].Text = dt.Rows[0][dt.Columns[i].ColumnName].ToString();
  251. }
  252. }
  253. }
  254. LeaveFindData = true;
  255. }
  256. else
  257. LeaveFindData = false;
  258. //不允许工单号为空的情况
  259. LockCheckBox.Checked = true;
  260. }
  261. }
  262. private void Search_Icon_Click(object sender, EventArgs e)
  263. {
  264. SearchIconClick?.Invoke(sender, new EventArgs());
  265. db = new DbFind(Name, tableName, selectField, setValueField, caller, formName, condition);
  266. db.Text = DBTitle1;
  267. LogManager.DoLog("DbFind查询,发起窗口【" + formName + "】,查询表【" + tableName + "】,字段" + selectField + ",条件" + condition);
  268. if (db.IsAbleDbFind1)
  269. {
  270. db.ShowDialog();
  271. }
  272. else
  273. {
  274. MessageBox.Show("该字段未配置DbFind", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  275. }
  276. }
  277. private void TextBox_Enter(object sender, EventArgs e)
  278. {
  279. TextBox.BackColor = System.Drawing.Color.GreenYellow;
  280. }
  281. /// <summary>
  282. /// 重新刷新数据
  283. /// </summary>
  284. /// <param name="sender"></param>
  285. /// <param name="e"></param>
  286. public void RefreshDB(object sender, EventArgs e)
  287. {
  288. TextBox_Leave(sender, e);
  289. }
  290. public void TextBox_Leave(object sender, EventArgs e)
  291. {
  292. TextBox.BackColor = System.Drawing.Color.White;
  293. GetData();
  294. }
  295. private void SearchTextBox_Load(object sender, EventArgs e)
  296. {
  297. if (!TextBoxEnable1)
  298. TextBox.BackColor = System.Drawing.Color.White;
  299. TextBox.Enabled = TextBoxEnable1;
  300. }
  301. private void SearchTextBox_SizeChanged(object sender, EventArgs e)
  302. {
  303. TextBox.Width = this.Width - Search_Icon.Width - 3;
  304. }
  305. }
  306. }