MaCodeSearchTextBox.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. using System;
  2. using System.Data;
  3. using System.Windows.Forms;
  4. namespace FileWatcher
  5. {
  6. public partial class MaCodeSearchTextBox : UserControl
  7. {
  8. #region 构造函数
  9. public MaCodeSearchTextBox()
  10. {
  11. InitializeComponent();
  12. }
  13. #endregion
  14. #region 变量
  15. /// <summary>
  16. /// 是否通过Leave事件找到数据
  17. /// </summary>
  18. public bool LeaveFindData;
  19. /// <summary>
  20. /// DBFind窗体弹出的标题
  21. /// </summary>
  22. private string DBTitle1;
  23. /// <summary>
  24. /// 输入框是否可编辑
  25. /// </summary>
  26. private bool TextBoxEnable1;
  27. /// <summary>
  28. /// 业务逻辑的标识符
  29. /// </summary>
  30. private string caller;
  31. /// <summary>
  32. /// 发起DbFind的窗体
  33. /// </summary>
  34. private string formName;
  35. /// <summary>
  36. /// 需要赋值的字段
  37. /// </summary>
  38. private string[] setValueField;
  39. /// <summary>
  40. /// 初始的筛选条件
  41. /// </summary>
  42. private string condition;
  43. /// <summary>
  44. /// 权限标识
  45. /// </summary>
  46. private string Power1;
  47. /// <summary>
  48. /// 查询的表名
  49. /// </summary>
  50. private string tableName;
  51. /// <summary>
  52. /// 查询的字段
  53. /// </summary>
  54. private string selectField;
  55. /// <summary>
  56. /// 设置绑定的CheckBox
  57. /// </summary>
  58. LockCheckBox LockCheckBox;
  59. public delegate void DBSourceChange(object sender, EventArgs e);
  60. public event DBSourceChange DbChange;
  61. DataTable dt = new DataTable();
  62. DbFind db;
  63. DataTable ReturnData1;
  64. public override string Text
  65. {
  66. get
  67. {
  68. return TextBox.Text.ToUpper();
  69. }
  70. set
  71. {
  72. TextBox.Text = value;
  73. }
  74. }
  75. string AllPower1;
  76. public string AllPower
  77. {
  78. get
  79. {
  80. return AllPower1;
  81. }
  82. set
  83. {
  84. AllPower1 = value;
  85. }
  86. }
  87. public string Caller
  88. {
  89. get
  90. {
  91. return caller;
  92. }
  93. set
  94. {
  95. caller = value;
  96. }
  97. }
  98. public string FormName
  99. {
  100. get
  101. {
  102. return formName;
  103. }
  104. set
  105. {
  106. formName = value;
  107. }
  108. }
  109. public string[] SetValueField
  110. {
  111. get
  112. {
  113. return setValueField;
  114. }
  115. set
  116. {
  117. setValueField = value;
  118. }
  119. }
  120. public string Condition
  121. {
  122. get
  123. {
  124. return condition;
  125. }
  126. set
  127. {
  128. condition = value;
  129. }
  130. }
  131. public string TableName
  132. {
  133. get
  134. {
  135. return tableName;
  136. }
  137. set
  138. {
  139. tableName = value;
  140. }
  141. }
  142. public string SelectField
  143. {
  144. get
  145. {
  146. return selectField;
  147. }
  148. set
  149. {
  150. selectField = value;
  151. }
  152. }
  153. public string Power
  154. {
  155. get
  156. {
  157. return Power1;
  158. }
  159. set
  160. {
  161. Power1 = value;
  162. }
  163. }
  164. public string DBTitle
  165. {
  166. get
  167. {
  168. return DBTitle1;
  169. }
  170. set
  171. {
  172. DBTitle1 = value;
  173. }
  174. }
  175. public bool TextBoxEnable
  176. {
  177. get
  178. {
  179. return TextBoxEnable1;
  180. }
  181. set
  182. {
  183. TextBoxEnable1 = value;
  184. }
  185. }
  186. public DataTable ReturnData
  187. {
  188. get
  189. {
  190. return ReturnData1;
  191. }
  192. set
  193. {
  194. ReturnData1 = value;
  195. }
  196. }
  197. #endregion
  198. #region 自定义事件
  199. public delegate void OnTextChange(object sender, EventArgs e);
  200. //定义事件
  201. public event OnTextChange UserControlTextChanged;
  202. //定义委托
  203. public delegate void Icon_Click(object sender, EventArgs e);
  204. /// <summary>
  205. /// 图标点击事件
  206. /// </summary>
  207. public event Icon_Click SearchIconClick;
  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. GetData(true);
  219. }
  220. public void SetLockCheckBox(LockCheckBox ctl)
  221. {
  222. LockCheckBox = ctl;
  223. }
  224. public void GetData(bool LeaveOrEnter)
  225. {
  226. DataHelper dh = SystemInf.dh;
  227. string sql = "";
  228. //将查询到的结果返回界面
  229. if (TextBox.Text != "")
  230. {
  231. sql = "select " + BaseUtil.AddField(setValueField) + " from " + tableName + " where " + Name + "='" + TextBox.Text.ToUpper() + "'";
  232. }
  233. else if (db != null)
  234. {
  235. sql = "select " + BaseUtil.AddField(setValueField) + " from " + tableName + " where " + Name + "='" + db.TextBoxValue1 + "'";
  236. }
  237. if (condition != null)
  238. {
  239. sql += " and " + condition;
  240. }
  241. try
  242. {
  243. dt = (DataTable)dh.ExecuteSql(sql, "select");
  244. if (dt.Rows.Count > 0)
  245. ReturnDataToFrom();
  246. }
  247. catch (Exception e)
  248. {
  249. }
  250. if (LeaveOrEnter || TextBox.Text != "")
  251. LockCheckBox.Checked = true;
  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. private void Search_Icon_Click(object sender, EventArgs e)
  273. {
  274. SearchIconClick?.Invoke(sender, new EventArgs());
  275. db = new DbFind(Name, tableName, selectField, setValueField, caller, formName, condition);
  276. db.Text = DBTitle1;
  277. if (db.IsAbleDbFind1)
  278. {
  279. db.ShowDialog();
  280. GetData(true);
  281. }
  282. else
  283. {
  284. MessageBox.Show("该字段未配置DbFind", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  285. }
  286. }
  287. private void ReturnDataToFrom()
  288. {
  289. ReturnData1 = dt;
  290. DbChange?.Invoke(new object(), new EventArgs());
  291. }
  292. public void TextBox_Leave(object sender, EventArgs e)
  293. {
  294. //GetData(false);
  295. }
  296. private void SearchTextBox_Load(object sender, EventArgs e)
  297. {
  298. if (!TextBoxEnable1)
  299. TextBox.BackColor = System.Drawing.Color.White;
  300. TextBox.Enabled = TextBoxEnable1;
  301. }
  302. private void SearchTextBox_SizeChanged(object sender, EventArgs e)
  303. {
  304. TextBox.Width = this.Width - Search_Icon.Width - 3;
  305. }
  306. private void MaCodeSearchTextBox_Leave(object sender, EventArgs e)
  307. {
  308. if (TextBox.Text != "")
  309. GetData(false);
  310. }
  311. }
  312. }