SearchTextBox.cs 9.0 KB

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