dkGoodsSearchBox.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /*******************************************************************************
  2. * Copyright(c) 2014 dongke All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:dkGoodsSearchBox.cs
  5. * 2.功能描述:产品选择控件
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 庄天威 2014/09/15 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.DataModels;
  17. using Dongke.IBOSS.PRD.WCF.Proxys;
  18. using Dongke.IBOSS.PRD.WCF.Proxys.SystemModuleService;
  19. namespace Dongke.IBOSS.PRD.Client.Controls.SearchTextBox
  20. {
  21. /// <summary>
  22. /// 产品选择控件
  23. /// </summary>
  24. public partial class dkGoodsSearchBox : UserControl
  25. {
  26. #region 成员变量
  27. private S_CMN_006 _frmGoods;
  28. private bool _isEnabled = true; // 控件是否可用
  29. private DataTable _dataSource = null; // 数据源
  30. private int? _goodsID; // 产品ID
  31. private string _goodsIDS; //id集
  32. private string _goodsCode; // 产品Code
  33. private string _goodsName; // 产品名称
  34. private string _goodsSpecification; //产品规格
  35. private bool _isMustInput; // 控件是否是必须输入项目
  36. private bool _IsOnlyShowValid = true; // 是否只显示有效数据
  37. #endregion
  38. #region 构造函数
  39. public dkGoodsSearchBox()
  40. {
  41. InitializeComponent();
  42. this.ReadOnly = true;
  43. }
  44. #endregion
  45. #region 属性
  46. /// <summary>
  47. /// 是否只显示有效数据
  48. /// </summary>
  49. [Description("设置控件是否只显示有效数据。")]
  50. [DefaultValue(true)]
  51. public bool IsOnlyShowValid
  52. {
  53. get
  54. {
  55. return _IsOnlyShowValid;
  56. }
  57. set
  58. {
  59. _IsOnlyShowValid = value;
  60. }
  61. }
  62. /// <summary>
  63. /// 获取或者设定控件是否是必须输入项目
  64. /// </summary>
  65. [DefaultValue("False")]
  66. [Description("获取或者设定控件是否是必须输入项目。")]
  67. public bool IsMustInput
  68. {
  69. get
  70. {
  71. return _isMustInput;
  72. }
  73. set
  74. {
  75. _isMustInput = value;
  76. // 项目为必须输入项时,需要修改字体颜色
  77. if (_isMustInput)
  78. {
  79. this.lblGoods.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(155)))), ((int)(((byte)(39)))), ((int)(((byte)(39)))));
  80. }
  81. else
  82. {
  83. this.lblGoods.ForeColor = System.Drawing.SystemColors.ControlText;
  84. }
  85. }
  86. }
  87. [DefaultValue("产品")]
  88. [Description("获取或者设定Lable标签字符。")]
  89. public string Title
  90. {
  91. get
  92. {
  93. return this.lblGoods.Text;
  94. }
  95. set
  96. {
  97. this.lblGoods.Text = value;
  98. AdjustControl();
  99. }
  100. }
  101. [DefaultValue(false)]
  102. public bool IsDeleteSelf
  103. {
  104. get;
  105. set;
  106. }
  107. /// <summary>
  108. /// 获取或者设定控件的数据源。
  109. /// </summary>
  110. [Description("获取或者设定控件的数据源。")]
  111. public DataTable DataSource
  112. {
  113. get
  114. {
  115. return _dataSource;
  116. }
  117. set
  118. {
  119. _dataSource = value;
  120. }
  121. }
  122. /// <summary>
  123. /// 获取或者设定控件的尺寸大小。
  124. /// </summary>
  125. [Description("获取或者设定控件的尺寸大小。")]
  126. public new Size Size
  127. {
  128. get
  129. {
  130. return base.Size;
  131. }
  132. set
  133. {
  134. base.Size = value;
  135. AdjustControl();
  136. }
  137. }
  138. /// <summary>
  139. /// 获取或者设定控件的文本框的文本字符串。
  140. /// </summary>
  141. [Description("获取或者设定控件的文本框的文本字符串。")]
  142. public override string Text
  143. {
  144. get
  145. {
  146. return this.txtGoods.Text;
  147. }
  148. set
  149. {
  150. this.txtGoods.Text = value;
  151. }
  152. }
  153. /// <summary>
  154. /// 获取或者设定控件的文本框的背景颜色。
  155. /// </summary>
  156. [Description("获取或者设定控件的文本框的背景颜色。")]
  157. public Color TxtGoodsBackColor
  158. {
  159. get
  160. {
  161. return this.txtGoods.BackColor;
  162. }
  163. set
  164. {
  165. this.txtGoods.BackColor = value;
  166. }
  167. }
  168. /// <summary>
  169. /// 获取或者设定控件的产品ID。
  170. /// </summary>
  171. [Description("获取或者设定控件的产品D。")]
  172. public int? GoodsID
  173. {
  174. get
  175. {
  176. return _goodsID;
  177. }
  178. set
  179. {
  180. _goodsID = value;
  181. if (_goodsID != null && _goodsID != 0 && this.DataSource != null)
  182. {
  183. DataTable GoodsTable = this.DataSource.Copy();
  184. DataRow[] dataRows = GoodsTable.Select("GGoodsID = " + this._goodsID);
  185. if (dataRows.Length == 1)
  186. {
  187. this.GoodsCode = dataRows[0]["GGoodsCode"] as string;
  188. this.Text = dataRows[0]["GGoodsName"] as string;
  189. }
  190. }
  191. if (_goodsID == null)
  192. {
  193. this.Text = string.Empty;
  194. GoodsCode = string.Empty;
  195. GoodsName = string.Empty;
  196. }
  197. }
  198. }
  199. /// <summary>
  200. /// 获取或者设定控件的产品Codeid集
  201. /// </summary>
  202. [Description("获取或者设定控件的产品Codeid集。")]
  203. public string GoodsIDS
  204. {
  205. get
  206. {
  207. return _goodsIDS;
  208. }
  209. set
  210. {
  211. _goodsIDS = value;
  212. }
  213. }
  214. /// <summary>
  215. /// 获取或者设定控件的产品Code
  216. /// </summary>
  217. [Description("获取或者设定控件的产品Code。")]
  218. public string GoodsCode
  219. {
  220. get
  221. {
  222. return _goodsCode;
  223. }
  224. set
  225. {
  226. _goodsCode = value;
  227. }
  228. }
  229. /// <summary>
  230. /// 获取或者设定控件的产品名称
  231. /// </summary>
  232. [Description("获取或者设定控件的产品名称。")]
  233. public string GoodsName
  234. {
  235. get
  236. {
  237. return _goodsName;
  238. }
  239. set
  240. {
  241. _goodsName = value;
  242. }
  243. }
  244. public string GoodsSpecification
  245. {
  246. get
  247. {
  248. return _goodsSpecification;
  249. }
  250. set
  251. {
  252. _goodsSpecification = value;
  253. }
  254. }
  255. /// <summary>
  256. /// 获取或者设定控件是否可用。
  257. /// </summary>
  258. [System.ComponentModel.DefaultValue(null)]
  259. [Description("获取或者设定控件是否可用。")]
  260. public new bool Enabled
  261. {
  262. get
  263. {
  264. return _isEnabled;
  265. }
  266. set
  267. {
  268. _isEnabled = value;
  269. this.txtGoods.Enabled = _isEnabled;
  270. this.TabStop = _isEnabled;
  271. }
  272. }
  273. /// <summary>
  274. /// 获取或者设定控件是否只读。
  275. /// </summary>
  276. [System.ComponentModel.DefaultValue(null)]
  277. [Description("获取或者设定控件是否只读。")]
  278. public bool ReadOnly
  279. {
  280. set
  281. {
  282. this.txtGoods.ReadOnly = value;
  283. }
  284. }
  285. /// <summary>
  286. /// 范围权限
  287. /// </summary>
  288. public byte PurviewType
  289. {
  290. get;
  291. set;
  292. }
  293. /// <summary>
  294. /// 多选。
  295. /// </summary>
  296. [System.ComponentModel.DefaultValue(false)]
  297. [Description("多选。")]
  298. public bool SelectMore
  299. {
  300. get;
  301. set;
  302. }
  303. #endregion
  304. #region 定义委托事件
  305. public delegate void TextBoxChangedHandle(object sender, TextChangeEventArgs e);
  306. public event TextBoxChangedHandle GoodsValueChanged;
  307. #endregion
  308. #region 控件事件
  309. [Description("控件光标点击按钮事件。")]
  310. public new event EventHandler Click
  311. {
  312. add
  313. {
  314. this.btnSearch.Click += value;
  315. }
  316. remove
  317. {
  318. this.btnSearch.Click -= value;
  319. }
  320. }
  321. /// <summary>
  322. /// 控件尺寸大小改变事件
  323. /// </summary>
  324. /// <param name="sender"></param>
  325. /// <param name="e"></param>
  326. private void dkGoodsSearchBox_SizeChanged(object sender, System.EventArgs e)
  327. {
  328. AdjustControl();
  329. }
  330. /// <summary>
  331. /// 查询按钮按下事件
  332. /// </summary>
  333. /// <param name="sender"></param>
  334. /// <param name="e"></param>
  335. private void btnSearch_Click(object sender, System.EventArgs e)
  336. {
  337. // 如果属性是不可用,是不能进行点击事件的
  338. if (!Enabled)
  339. {
  340. return;
  341. }
  342. // 当数据源为null时,查询数据源
  343. if (DataSource == null)
  344. {
  345. // 查询数据源
  346. GoodsEntity ge = new GoodsEntity();
  347. ge.CeaseFlag = 1;
  348. ge.ValueFlag = 1;
  349. DataSource = SystemModuleProxy.Service.SerachGoods(ge).Tables[0];
  350. }
  351. // 释放窗体资源
  352. if (null != _frmGoods)
  353. {
  354. _frmGoods.Dispose();
  355. _frmGoods = null;
  356. }
  357. // 打开查询窗体
  358. if (this.SelectMore)
  359. {
  360. _frmGoods = new S_CMN_006(1);
  361. }
  362. else
  363. {
  364. _frmGoods = new S_CMN_006(0);
  365. }
  366. _frmGoods.DataSource = this.DataSource;
  367. DialogResult dialogResult = _frmGoods.ShowDialog();
  368. // 查询窗体返回值给控件赋值
  369. if (dialogResult.Equals(DialogResult.OK))
  370. {
  371. if (this.SelectMore)
  372. {
  373. _goodsCode = string.Empty;
  374. _goodsName = string.Empty;
  375. _goodsIDS = string.Empty;
  376. if (_frmGoods.dataDT.Rows.Count > 0 || _frmGoods.DataRow != null)
  377. {
  378. for (int i = 0; i < _frmGoods.dataDT.Rows.Count; i++)
  379. {
  380. if (_frmGoods.dataDT.Rows[i]["sel"].ToString().Equals("1"))
  381. {
  382. if (string.IsNullOrEmpty(_goodsIDS))
  383. {
  384. _goodsCode = _frmGoods.dataDT.Rows[i]["GoodsCODE"].ToString();
  385. _goodsName = _frmGoods.dataDT.Rows[i]["GoodsNAME"].ToString();
  386. _goodsIDS = _frmGoods.dataDT.Rows[i]["GoodsID"].ToString();
  387. }
  388. else
  389. {
  390. _goodsCode += "," + _frmGoods.dataDT.Rows[i]["GoodsCODE"].ToString();
  391. _goodsName += "," + _frmGoods.dataDT.Rows[i]["GoodsNAME"].ToString();
  392. _goodsIDS += "," + _frmGoods.dataDT.Rows[i]["GoodsID"].ToString();
  393. }
  394. }
  395. }
  396. this.Text = _goodsName;
  397. }
  398. }
  399. else
  400. {
  401. if (_frmGoods.DataRow != null)
  402. {
  403. _goodsID = Convert.ToInt32(_frmGoods.DataRow["GoodsID"]);
  404. _goodsCode = _frmGoods.DataRow["GoodsCODE"].ToString();
  405. _goodsName = _frmGoods.DataRow["GoodsNAME"].ToString();
  406. _goodsSpecification = _frmGoods.DataRow["GoodsSpecification"].ToString();
  407. this.Text = _frmGoods.DataRow["GoodsNAME"].ToString();
  408. }
  409. }
  410. }
  411. }
  412. /// <summary>
  413. /// 清除控件的值
  414. /// </summary>
  415. /// <param name="sender"></param>
  416. /// <param name="e"></param>
  417. private void txtGoods_KeyDown(object sender, KeyEventArgs e)
  418. {
  419. if (e.KeyCode == Keys.Back || e.KeyCode == Keys.Delete)
  420. {
  421. ClearControl();
  422. }
  423. }
  424. private void txtGoodsType_TextChanged(object sender, EventArgs e)
  425. {
  426. if (GoodsValueChanged != null)
  427. {
  428. GoodsValueChanged(this, new TextChangeEventArgs(this.txtGoods.Text));
  429. }
  430. this.OnTextChanged(e);
  431. }
  432. public class TextChangeEventArgs : EventArgs
  433. {
  434. public TextChangeEventArgs(string message)
  435. {
  436. }
  437. }
  438. #endregion
  439. #region 私有方法/函数
  440. /// <summary>
  441. /// 根据控件的尺寸变化,改变控件内部控件的尺寸大小
  442. /// </summary>
  443. protected void AdjustControl()
  444. {
  445. // 取得按钮控件的宽度和高度
  446. int buttonWidth = (this.btnSearch.Visible) ? this.btnSearch.Width : 0;
  447. this.Height = this.btnSearch.Height;
  448. // 设置控件的尺寸和位置。
  449. this.txtGoods.Location = new System.Drawing.Point(this.lblGoods.Width +
  450. ControlsConst.CONTROLSPACE, (this.Height - this.txtGoods.Height) / 2);
  451. this.txtGoods.Size = new System.Drawing.Size(this.Width - this.btnSearch.Width -
  452. (this.lblGoods.Width + ControlsConst.CONTROLSPACE * 2), this.Height);
  453. this.btnSearch.Location =
  454. new System.Drawing.Point(this.Width - this.btnSearch.Width,
  455. (this.Height - this.btnSearch.Height) / 2);
  456. // 设置标签的尺寸和位置。
  457. this.lblGoods.Location =
  458. new System.Drawing.Point(0, (this.Height - this.lblGoods.Height) / 2);
  459. this.lblGoods.Size = new System.Drawing.Size(this.lblGoods.Width, this.lblGoods.Height);
  460. }
  461. /// <summary>
  462. /// 清空控件的值
  463. /// </summary>
  464. /// <param name="dataSource"></param>
  465. /// <returns></returns>
  466. public void ClearControl()
  467. {
  468. this.txtGoods.Text = "";
  469. this._goodsID = null;
  470. this._goodsCode = string.Empty;
  471. this._goodsName = string.Empty;
  472. this._goodsIDS = string.Empty;
  473. }
  474. /// <summary>
  475. /// 过滤数据源
  476. /// </summary>
  477. /// <param name="dataSource"></param>
  478. /// <returns></returns>
  479. protected DataTable SetFilter(DataTable dataSource)
  480. {
  481. //string filter = "ValueFlag = 1";
  482. //if (dataSource is DataTable)
  483. //{
  484. // DataTable dtbl = (DataTable)dataSource;
  485. // // 若设置了只显示有效数据
  486. // if (this._IsOnlyShowValid == true)
  487. // {
  488. // dtbl.DefaultView.RowFilter = filter;
  489. // }
  490. // dataSource = dtbl.DefaultView.ToTable();
  491. //}
  492. return dataSource;
  493. }
  494. #endregion
  495. }
  496. }