SearchTextBox.cs 8.2 KB

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