SearchTextBox.cs 9.9 KB

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