dkPostSearchBox.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /*******************************************************************************
  2. * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:dkPostSearchBox.cs
  5. * 2.功能描述:职务选择控件
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 冯雪 2014/09/13 1.00 新建
  9. *******************************************************************************/
  10. using System;
  11. using System.ComponentModel;
  12. using System.Data;
  13. using System.Drawing;
  14. using System.Windows.Forms;
  15. using Dongke.IBOSS.PRD.Basics.BaseControls;
  16. using Dongke.IBOSS.PRD.WCF.Proxys;
  17. namespace Dongke.IBOSS.PRD.Client.Controls
  18. {
  19. /// <summary>
  20. /// 职务选择控件
  21. /// </summary>
  22. public partial class dkPostSearchBox : UserControl
  23. {
  24. #region 成员变量
  25. /// <summary>
  26. /// 数据选择窗体
  27. /// </summary>
  28. private S_CMN_004 _frmPost;
  29. /// <summary>
  30. /// 控件是否可用
  31. /// </summary>
  32. private bool _isEnabled = true;
  33. /// <summary>
  34. /// 数据源
  35. /// </summary>
  36. private DataTable _dataSource = null;
  37. /// <summary>
  38. /// 职务ID
  39. /// </summary>
  40. private int? _postID;
  41. /// <summary>
  42. /// 职务编号
  43. /// </summary>
  44. private string _postCode;
  45. /// <summary>
  46. /// 职务名称
  47. /// </summary>
  48. private string _postName;
  49. /// <summary>
  50. /// 控件是否是必须输入项目
  51. /// </summary>
  52. private bool _isMustInput;
  53. /// <summary>
  54. /// 是否只显示有效数据
  55. /// </summary>
  56. private bool _IsOnlyShowValid = true;
  57. #endregion
  58. #region 构造函数
  59. public dkPostSearchBox()
  60. {
  61. InitializeComponent();
  62. this.ReadOnly = true;
  63. }
  64. #endregion
  65. #region 属性
  66. /// <summary>
  67. /// 是否只显示有效数据
  68. /// </summary>
  69. [Description("设置控件是否只显示有效数据。")]
  70. [DefaultValue(true)]
  71. public bool IsOnlyShowValid
  72. {
  73. get
  74. {
  75. return _IsOnlyShowValid;
  76. }
  77. set
  78. {
  79. _IsOnlyShowValid = value;
  80. }
  81. }
  82. /// <summary>
  83. /// 获取或者设定控件是否是必须输入项目
  84. /// </summary>
  85. [DefaultValue("False")]
  86. [Description("获取或者设定控件是否是必须输入项目。")]
  87. public bool IsMustInput
  88. {
  89. get
  90. {
  91. return _isMustInput;
  92. }
  93. set
  94. {
  95. _isMustInput = value;
  96. // 项目为必须输入项时,需要修改字体颜色
  97. if (_isMustInput)
  98. {
  99. this.lblPost.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(155)))), ((int)(((byte)(39)))), ((int)(((byte)(39)))));
  100. }
  101. else
  102. {
  103. this.lblPost.ForeColor = System.Drawing.SystemColors.ControlText;
  104. }
  105. }
  106. }
  107. /// <summary>
  108. /// Lable标签文本
  109. /// </summary>
  110. [DefaultValue("职务名称")]
  111. [Description("获取或者设定Lable标签字符。")]
  112. public string Title
  113. {
  114. get
  115. {
  116. return this.lblPost.Text;
  117. }
  118. set
  119. {
  120. this.lblPost.Text = value;
  121. AdjustControl();
  122. }
  123. }
  124. /// <summary>
  125. /// 获取或者设定控件的数据源。
  126. /// </summary>
  127. [Description("获取或者设定控件的数据源。")]
  128. public DataTable DataSource
  129. {
  130. get
  131. {
  132. return _dataSource;
  133. }
  134. set
  135. {
  136. DataTable dataSource = SetFilter(value);
  137. _dataSource = dataSource;
  138. }
  139. }
  140. /// <summary>
  141. /// 获取或者设定控件的尺寸大小。
  142. /// </summary>
  143. [Description("获取或者设定控件的尺寸大小。")]
  144. public new Size Size
  145. {
  146. get
  147. {
  148. return base.Size;
  149. }
  150. set
  151. {
  152. base.Size = value;
  153. AdjustControl();
  154. }
  155. }
  156. /// <summary>
  157. /// 获取或者设定控件的文本框的文本字符串。
  158. /// </summary>
  159. [Description("获取或者设定控件的文本框的文本字符串。")]
  160. public override string Text
  161. {
  162. get
  163. {
  164. return this.txtPost.Text;
  165. }
  166. set
  167. {
  168. this.txtPost.Text = value;
  169. }
  170. }
  171. /// <summary>
  172. /// 获取或者设定控件的文本框的背景颜色。
  173. /// </summary>
  174. [Description("获取或者设定控件的文本框的背景颜色。")]
  175. public Color TxtUserBackColor
  176. {
  177. get
  178. {
  179. return this.txtPost.BackColor;
  180. }
  181. set
  182. {
  183. this.txtPost.BackColor = value;
  184. }
  185. }
  186. /// <summary>
  187. /// 获取或者设定控件的职务ID。
  188. /// </summary>
  189. [Description("获取或者设定控件的职务ID。")]
  190. [DefaultValue(null)]
  191. public int? PostID
  192. {
  193. get
  194. {
  195. return _postID;
  196. }
  197. set
  198. {
  199. this._postID = null;
  200. this._postCode = string.Empty;
  201. this._postName = string.Empty;
  202. this.Text = string.Empty;
  203. if (value != null)
  204. {
  205. // 当数据源为null时,查询数据源
  206. if (DataSource == null)
  207. {
  208. // 查询数据源
  209. DataSource = GetPostInfo();
  210. }
  211. if (value != null && value != 0 && this.DataSource != null)
  212. {
  213. DataTable userTable = this.DataSource.Copy();
  214. DataRow[] dataRows = userTable.Select("PostID = " + value);
  215. if (dataRows.Length == 1)
  216. {
  217. this._postID = value;
  218. this._postCode = dataRows[0]["PostCode"] as string;
  219. this._postName = dataRows[0]["PostName"] as string;
  220. this.Text = dataRows[0]["PostName"] as string;
  221. }
  222. }
  223. }
  224. }
  225. }
  226. /// <summary>
  227. /// 获取或者设定控件的职务Code
  228. /// </summary>
  229. [Description("获取或者设定控件的职务Code。")]
  230. public string PostCode
  231. {
  232. get
  233. {
  234. return _postCode;
  235. }
  236. set
  237. {
  238. _postCode = value;
  239. }
  240. }
  241. /// <summary>
  242. /// 获取或者设定控件的职务名称
  243. /// </summary>
  244. [Description("获取或者设定控件的职务名称。")]
  245. public string PostName
  246. {
  247. get
  248. {
  249. return _postName;
  250. }
  251. set
  252. {
  253. _postName = value;
  254. }
  255. }
  256. /// <summary>
  257. /// 获取或者设定控件是否可用。
  258. /// </summary>
  259. [System.ComponentModel.DefaultValue(null)]
  260. [Description("获取或者设定控件是否可用。")]
  261. public new bool Enabled
  262. {
  263. get
  264. {
  265. return _isEnabled;
  266. }
  267. set
  268. {
  269. _isEnabled = value;
  270. this.txtPost.Enabled = _isEnabled;
  271. this.TabStop = _isEnabled;
  272. }
  273. }
  274. /// <summary>
  275. /// 获取或者设定控件是否只读。
  276. /// </summary>
  277. [System.ComponentModel.DefaultValue(null)]
  278. [Description("获取或者设定控件是否只读。")]
  279. public bool ReadOnly
  280. {
  281. set
  282. {
  283. this.txtPost.ReadOnly = value;
  284. }
  285. }
  286. /// <summary>
  287. /// 范围权限
  288. /// </summary>
  289. public byte PurviewType
  290. {
  291. get;
  292. set;
  293. }
  294. #endregion
  295. #region 定义委托事件
  296. public delegate void TextBoxChangedHandle(object sender, TextChangeEventArgs e);
  297. public event TextBoxChangedHandle UserValueChanged;
  298. #endregion
  299. #region 控件事件
  300. /// <summary>
  301. /// 控件尺寸大小改变事件
  302. /// </summary>
  303. /// <param name="sender"></param>
  304. /// <param name="e"></param>
  305. private void dkPostSearchBox_SizeChanged(object sender, System.EventArgs e)
  306. {
  307. AdjustControl();
  308. }
  309. /// <summary>
  310. /// 查询按钮按下事件
  311. /// </summary>
  312. /// <param name="sender"></param>
  313. /// <param name="e"></param>
  314. private void btnSearch_Click(object sender, System.EventArgs e)
  315. {
  316. // 如果属性是不可用,是不能进行点击事件的
  317. if (!Enabled)
  318. {
  319. return;
  320. }
  321. // 当数据源为null时,查询数据源
  322. if (DataSource == null)
  323. {
  324. // 查询数据源
  325. DataSource = GetPostInfo();
  326. }
  327. // 释放窗体资源
  328. if (null != _frmPost)
  329. {
  330. _frmPost.Dispose();
  331. _frmPost = null;
  332. }
  333. // 打开查询窗体
  334. _frmPost = new S_CMN_004(this.txtPost.Text);
  335. _frmPost.DataSource = this.DataSource;
  336. DialogResult dialogResult = _frmPost.ShowDialog();
  337. // 查询窗体返回值给控件赋值
  338. if (dialogResult.Equals(DialogResult.OK))
  339. {
  340. if (_frmPost.PostRow != null)
  341. {
  342. _postID = Convert.ToInt32(_frmPost.PostRow["PostID"]);
  343. _postCode = _frmPost.PostRow["PostCode"].ToString();
  344. _postName = _frmPost.PostRow["PostName"].ToString();
  345. this.Text = _frmPost.PostRow["PostName"].ToString();
  346. }
  347. }
  348. }
  349. /// <summary>
  350. /// 清除控件的值
  351. /// </summary>
  352. /// <param name="sender"></param>
  353. /// <param name="e"></param>
  354. private void txtUser_KeyDown(object sender, KeyEventArgs e)
  355. {
  356. if (e.KeyCode == Keys.Back || e.KeyCode == Keys.Delete)
  357. {
  358. this.txtPost.Text = "";
  359. this._postID = null;
  360. this._postCode = string.Empty;
  361. this._postName = string.Empty;
  362. }
  363. }
  364. /// <summary>
  365. /// 文本框文本改变事件
  366. /// </summary>
  367. /// <param name="sender"></param>
  368. /// <param name="e"></param>
  369. private void txtUser_TextChanged(object sender, EventArgs e)
  370. {
  371. if (UserValueChanged != null)
  372. {
  373. UserValueChanged(this, new TextChangeEventArgs(this.txtPost.Text));
  374. }
  375. }
  376. public class TextChangeEventArgs : EventArgs
  377. {
  378. public TextChangeEventArgs(string message)
  379. {
  380. }
  381. }
  382. #endregion
  383. #region 私有方法
  384. /// <summary>
  385. /// 设置控件的样式
  386. /// </summary>
  387. protected void AdjustControl()
  388. {
  389. // 取得按钮控件的宽度和高度
  390. int buttonWidth = (this.btnSearch.Visible) ? this.btnSearch.Width : 0;
  391. this.Height = this.btnSearch.Height;
  392. // 设置控件的尺寸和位置。
  393. this.txtPost.Location = new System.Drawing.Point(this.lblPost.Width +
  394. ControlsConst.CONTROLSPACE, (this.Height - this.txtPost.Height) / 2);
  395. this.txtPost.Size = new System.Drawing.Size(this.Width - this.btnSearch.Width -
  396. (this.lblPost.Width + ControlsConst.CONTROLSPACE * 2), this.Height);
  397. this.btnSearch.Location =
  398. new System.Drawing.Point(this.Width - this.btnSearch.Width,
  399. (this.Height - this.btnSearch.Height) / 2);
  400. // 设置标签的尺寸和位置。
  401. this.lblPost.Location =
  402. new System.Drawing.Point(0, (this.Height - this.lblPost.Height) / 2);
  403. this.lblPost.Size = new System.Drawing.Size(this.lblPost.Width, this.lblPost.Height);
  404. }
  405. /// <summary>
  406. /// 过滤数据源
  407. /// </summary>
  408. /// <param name="dataSource"></param>
  409. /// <returns></returns>
  410. protected DataTable SetFilter(DataTable dataSource)
  411. {
  412. return dataSource;
  413. }
  414. /// <summary>
  415. /// 调用服务方法获取用户信息
  416. /// </summary>
  417. /// <returns></returns>
  418. private DataTable GetPostInfo()
  419. {
  420. DataSet userDataSet = CommonModuleProxy.Service.GetMSTPostInfo();
  421. if (userDataSet == null)
  422. {
  423. return new DataTable();
  424. }
  425. if (userDataSet.Tables.Count <= 0)
  426. {
  427. return new DataTable();
  428. }
  429. return userDataSet.Tables[0];
  430. }
  431. #endregion
  432. }
  433. }