SearchTextBox.cs 8.4 KB

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