UCCombox.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 08-08-2019
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCCombox.cs">
  7. // Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
  8. // </copyright>
  9. //
  10. // Blog: https://www.cnblogs.com/bfyx
  11. // GitHub:https://github.com/kwwwvagaa/NetWinformControl
  12. // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
  13. //
  14. // If you use this code, please keep this note.
  15. // ***********************************************************************
  16. using System;
  17. using System.Collections.Generic;
  18. using System.ComponentModel;
  19. using System.Drawing;
  20. using System.Data;
  21. using System.Linq;
  22. using System.Text;
  23. using System.Windows.Forms;
  24. using System.Drawing.Drawing2D;
  25. namespace HZH_Controls.Controls
  26. {
  27. /// <summary>
  28. /// Class UCCombox.
  29. /// Implements the <see cref="HZH_Controls.Controls.UCControlBase" />
  30. /// </summary>
  31. /// <seealso cref="HZH_Controls.Controls.UCControlBase" />
  32. [DefaultEvent("SelectedChangedEvent")]
  33. public partial class UCCombox : UCControlBase
  34. {
  35. /// <summary>
  36. /// The fore color
  37. /// </summary>
  38. Color _ForeColor = Color.FromArgb(64, 64, 64);
  39. /// <summary>
  40. /// 文字颜色
  41. /// </summary>
  42. /// <value>The color of the fore.</value>
  43. /// <PermissionSet>
  44. /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  45. /// </PermissionSet>
  46. [Description("文字颜色"), Category("自定义")]
  47. public override Color ForeColor
  48. {
  49. get
  50. {
  51. return _ForeColor;
  52. }
  53. set
  54. {
  55. _ForeColor = value;
  56. lblInput.ForeColor = value;
  57. txtInput.ForeColor = value;
  58. }
  59. }
  60. /// <summary>
  61. /// 选中事件
  62. /// </summary>
  63. [Description("选中事件"), Category("自定义")]
  64. public event EventHandler SelectedChangedEvent;
  65. /// <summary>
  66. /// 文本改变事件
  67. /// </summary>
  68. [Description("文本改变事件"), Category("自定义")]
  69. public event EventHandler TextChangedEvent;
  70. /// <summary>
  71. /// The box style
  72. /// </summary>
  73. private ComboBoxStyle _BoxStyle = ComboBoxStyle.DropDown;
  74. /// <summary>
  75. /// 控件样式
  76. /// </summary>
  77. /// <value>The box style.</value>
  78. [Description("控件样式"), Category("自定义")]
  79. public ComboBoxStyle BoxStyle
  80. {
  81. get { return _BoxStyle; }
  82. set
  83. {
  84. _BoxStyle = value;
  85. if (value == ComboBoxStyle.DropDownList)
  86. {
  87. lblInput.Visible = true;
  88. txtInput.Visible = false;
  89. }
  90. else
  91. {
  92. lblInput.Visible = false;
  93. txtInput.Visible = true;
  94. }
  95. if (this._BoxStyle == ComboBoxStyle.DropDownList)
  96. {
  97. txtInput.BackColor = _BackColor;
  98. base.FillColor = _BackColor;
  99. base.RectColor = _BackColor;
  100. }
  101. else
  102. {
  103. txtInput.BackColor = Color.White;
  104. base.FillColor = Color.White;
  105. base.RectColor = Color.FromArgb(220, 220, 220);
  106. }
  107. }
  108. }
  109. /// <summary>
  110. /// The font
  111. /// </summary>
  112. private Font _Font = new Font("微软雅黑", 12);
  113. /// <summary>
  114. /// 字体
  115. /// </summary>
  116. /// <value>The font.</value>
  117. /// <PermissionSet>
  118. /// <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  119. /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  120. /// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />
  121. /// <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  122. /// </PermissionSet>
  123. [Description("字体"), Category("自定义")]
  124. public new Font Font
  125. {
  126. get { return _Font; }
  127. set
  128. {
  129. _Font = value;
  130. lblInput.Font = value;
  131. txtInput.Font = value;
  132. txtInput.PromptFont = value;
  133. this.txtInput.Location = new Point(this.txtInput.Location.X, (this.Height - txtInput.Height) / 2);
  134. this.lblInput.Location = new Point(this.lblInput.Location.X, (this.Height - lblInput.Height) / 2);
  135. }
  136. }
  137. /// <summary>
  138. /// 当使用边框时填充颜色,当值为背景色或透明色或空值则不填充
  139. /// </summary>
  140. /// <value>The color of the fill.</value>
  141. [Obsolete("不再可用的属性")]
  142. [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
  143. private new Color FillColor
  144. {
  145. get;
  146. set;
  147. }
  148. /// <summary>
  149. /// 边框颜色
  150. /// </summary>
  151. /// <value>The color of the rect.</value>
  152. [Obsolete("不再可用的属性")]
  153. [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
  154. private new Color RectColor
  155. {
  156. get;
  157. set;
  158. }
  159. /// <summary>
  160. /// The text value
  161. /// </summary>
  162. private string _TextValue;
  163. /// <summary>
  164. /// 文字
  165. /// </summary>
  166. /// <value>The text value.</value>
  167. [Description("文字"), Category("自定义")]
  168. public string TextValue
  169. {
  170. get { return _TextValue; }
  171. set
  172. {
  173. _TextValue = value;
  174. if (lblInput.Text != value)
  175. lblInput.Text = value;
  176. if (txtInput.Text != value)
  177. txtInput.Text = value;
  178. }
  179. }
  180. /// <summary>
  181. /// The source
  182. /// </summary>
  183. private List<KeyValuePair<string, string>> _source = null;
  184. /// <summary>
  185. /// 数据源
  186. /// </summary>
  187. /// <value>The source.</value>
  188. [Description("数据源"), Category("自定义")]
  189. public List<KeyValuePair<string, string>> Source
  190. {
  191. get { return _source; }
  192. set
  193. {
  194. _source = value;
  195. _selectedIndex = -1;
  196. _selectedValue = "";
  197. _selectedItem = new KeyValuePair<string, string>();
  198. _selectedText = "";
  199. lblInput.Text = "";
  200. txtInput.Text = "";
  201. }
  202. }
  203. /// <summary>
  204. /// The selected item
  205. /// </summary>
  206. private KeyValuePair<string, string> _selectedItem = new KeyValuePair<string, string>();
  207. /// <summary>
  208. /// The selected index
  209. /// </summary>
  210. private int _selectedIndex = -1;
  211. /// <summary>
  212. /// 选中的数据下标
  213. /// </summary>
  214. /// <value>The index of the selected.</value>
  215. [Description("选中的数据下标"), Category("自定义")]
  216. public int SelectedIndex
  217. {
  218. get
  219. {
  220. return _selectedIndex;
  221. }
  222. set
  223. {
  224. if (value < 0 || _source == null || _source.Count <= 0 || value >= _source.Count)
  225. {
  226. _selectedIndex = -1;
  227. _selectedValue = "";
  228. _selectedItem = new KeyValuePair<string, string>();
  229. SelectedText = "";
  230. }
  231. else
  232. {
  233. _selectedIndex = value;
  234. _selectedItem = _source[value];
  235. _selectedValue = _source[value].Key;
  236. SelectedText = _source[value].Value;
  237. }
  238. }
  239. }
  240. /// <summary>
  241. /// The selected value
  242. /// </summary>
  243. private string _selectedValue = "";
  244. /// <summary>
  245. /// 选中的值
  246. /// </summary>
  247. /// <value>The selected value.</value>
  248. [Description("选中的值"), Category("自定义")]
  249. public string SelectedValue
  250. {
  251. get
  252. {
  253. return _selectedValue;
  254. }
  255. set
  256. {
  257. if (_source == null || _source.Count <= 0)
  258. {
  259. SelectedText = "";
  260. _selectedValue = "";
  261. _selectedIndex = -1;
  262. _selectedItem = new KeyValuePair<string, string>();
  263. }
  264. else
  265. {
  266. for (int i = 0; i < _source.Count; i++)
  267. {
  268. if (_source[i].Key == value)
  269. {
  270. _selectedValue = value;
  271. _selectedIndex = i;
  272. _selectedItem = _source[i];
  273. SelectedText = _source[i].Value;
  274. return;
  275. }
  276. }
  277. _selectedValue = "";
  278. _selectedIndex = -1;
  279. _selectedItem = new KeyValuePair<string, string>();
  280. SelectedText = "";
  281. }
  282. }
  283. }
  284. /// <summary>
  285. /// The selected text
  286. /// </summary>
  287. private string _selectedText = "";
  288. /// <summary>
  289. /// 选中的文本
  290. /// </summary>
  291. /// <value>The selected text.</value>
  292. [Description("选中的文本"), Category("自定义")]
  293. public string SelectedText
  294. {
  295. get { return _selectedText; }
  296. private set
  297. {
  298. _selectedText = value;
  299. lblInput.Text = _selectedText;
  300. txtInput.Text = _selectedText;
  301. if (SelectedChangedEvent != null)
  302. {
  303. SelectedChangedEvent(this, null);
  304. }
  305. }
  306. }
  307. /// <summary>
  308. /// The item width
  309. /// </summary>
  310. private int _ItemWidth = 70;
  311. /// <summary>
  312. /// 项宽度
  313. /// </summary>
  314. /// <value>The width of the item.</value>
  315. [Description("项宽度"), Category("自定义")]
  316. public int ItemWidth
  317. {
  318. get { return _ItemWidth; }
  319. set { _ItemWidth = value; }
  320. }
  321. /// <summary>
  322. /// The drop panel height
  323. /// </summary>
  324. private int _dropPanelHeight = -1;
  325. /// <summary>
  326. /// 下拉面板高度
  327. /// </summary>
  328. /// <value>The height of the drop panel.</value>
  329. [Description("下拉面板高度"), Category("自定义")]
  330. public int DropPanelHeight
  331. {
  332. get { return _dropPanelHeight; }
  333. set { _dropPanelHeight = value; }
  334. }
  335. /// <summary>
  336. /// 获取或设置控件的背景色。
  337. /// </summary>
  338. /// <value>The color of the back.</value>
  339. /// <PermissionSet>
  340. /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  341. /// </PermissionSet>
  342. [Obsolete("不再可用的属性,如需要改变背景色,请使用BackColorExt")]
  343. [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
  344. private new Color BackColor
  345. {
  346. get
  347. {
  348. return base.BackColor;
  349. }
  350. set
  351. {
  352. base.BackColor = Color.Transparent;
  353. }
  354. }
  355. /// <summary>
  356. /// The back color
  357. /// </summary>
  358. private Color _BackColor = Color.FromArgb(240, 240, 240);
  359. /// <summary>
  360. /// 背景色
  361. /// </summary>
  362. /// <value>The back color ext.</value>
  363. [Description("背景色"), Category("自定义")]
  364. public Color BackColorExt
  365. {
  366. get
  367. {
  368. return _BackColor;
  369. }
  370. set
  371. {
  372. if (value == Color.Transparent)
  373. return;
  374. _BackColor = value;
  375. lblInput.BackColor = value;
  376. if (this._BoxStyle == ComboBoxStyle.DropDownList)
  377. {
  378. txtInput.BackColor = value;
  379. base.FillColor = value;
  380. base.RectColor = value;
  381. }
  382. else
  383. {
  384. txtInput.BackColor = Color.White;
  385. base.FillColor = Color.White;
  386. base.RectColor = Color.FromArgb(220, 220, 220);
  387. }
  388. }
  389. }
  390. /// <summary>
  391. /// The triangle color
  392. /// </summary>
  393. private Color triangleColor = Color.FromArgb(255, 77, 59);
  394. /// <summary>
  395. /// 三角颜色
  396. /// </summary>
  397. /// <value>The color of the triangle.</value>
  398. [Description("三角颜色"), Category("自定义")]
  399. public Color TriangleColor
  400. {
  401. get { return triangleColor; }
  402. set
  403. {
  404. triangleColor = value;
  405. Bitmap bit = new Bitmap(12, 10);
  406. Graphics g = Graphics.FromImage(bit);
  407. g.SetGDIHigh();
  408. GraphicsPath path = new GraphicsPath();
  409. path.AddLines(new Point[]
  410. {
  411. new Point(1,1),
  412. new Point(11,1),
  413. new Point(6,10),
  414. new Point(1,1)
  415. });
  416. g.FillPath(new SolidBrush(value), path);
  417. g.Dispose();
  418. panel1.BackgroundImage = bit;
  419. }
  420. }
  421. /// <summary>
  422. /// Initializes a new instance of the <see cref="UCCombox" /> class.
  423. /// </summary>
  424. public UCCombox()
  425. {
  426. InitializeComponent();
  427. lblInput.BackColor = _BackColor;
  428. if (this._BoxStyle == ComboBoxStyle.DropDownList)
  429. {
  430. txtInput.BackColor = _BackColor;
  431. base.FillColor = _BackColor;
  432. base.RectColor = _BackColor;
  433. }
  434. else
  435. {
  436. txtInput.BackColor = Color.White;
  437. base.FillColor = Color.White;
  438. base.RectColor = Color.FromArgb(220, 220, 220);
  439. }
  440. base.BackColor = Color.Transparent;
  441. }
  442. /// <summary>
  443. /// Handles the SizeChanged event of the UCComboBox control.
  444. /// </summary>
  445. /// <param name="sender">The source of the event.</param>
  446. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  447. private void UCComboBox_SizeChanged(object sender, EventArgs e)
  448. {
  449. this.txtInput.Location = new Point(this.txtInput.Location.X, (this.Height - txtInput.Height) / 2);
  450. this.lblInput.Location = new Point(this.lblInput.Location.X, (this.Height - lblInput.Height) / 2);
  451. }
  452. /// <summary>
  453. /// Handles the TextChanged event of the txtInput control.
  454. /// </summary>
  455. /// <param name="sender">The source of the event.</param>
  456. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  457. private void txtInput_TextChanged(object sender, EventArgs e)
  458. {
  459. TextValue = txtInput.Text;
  460. if (TextChangedEvent != null)
  461. {
  462. TextChangedEvent(this, null);
  463. }
  464. }
  465. /// <summary>
  466. /// Handles the MouseDown event of the click control.
  467. /// </summary>
  468. /// <param name="sender">The source of the event.</param>
  469. /// <param name="e">The <see cref="MouseEventArgs" /> instance containing the event data.</param>
  470. protected virtual void click_MouseDown(object sender, MouseEventArgs e)
  471. {
  472. if (_frmAnchor == null || _frmAnchor.IsDisposed || _frmAnchor.Visible == false)
  473. {
  474. if (this.Source != null && this.Source.Count > 0)
  475. {
  476. int intRow = 0;
  477. int intCom = 1;
  478. var p = this.PointToScreen(this.Location);
  479. while (true)
  480. {
  481. int intScreenHeight = Screen.PrimaryScreen.Bounds.Height;
  482. if ((p.Y + this.Height + this.Source.Count / intCom * 50 < intScreenHeight || p.Y - this.Source.Count / intCom * 50 > 0)
  483. && (_dropPanelHeight <= 0 ? true : (this.Source.Count / intCom * 50 <= _dropPanelHeight)))
  484. {
  485. intRow = this.Source.Count / intCom + (this.Source.Count % intCom != 0 ? 1 : 0);
  486. break;
  487. }
  488. intCom++;
  489. }
  490. UCTimePanel ucTime = new UCTimePanel();
  491. ucTime.IsShowBorder = true;
  492. int intWidth = this.Width / intCom;
  493. if (intWidth < _ItemWidth)
  494. intWidth = _ItemWidth;
  495. Size size = new Size(intCom * intWidth, intRow * 50);
  496. ucTime.Size = size;
  497. ucTime.FirstEvent = true;
  498. ucTime.SelectSourceEvent += ucTime_SelectSourceEvent;
  499. ucTime.Row = intRow;
  500. ucTime.Column = intCom;
  501. List<KeyValuePair<string, string>> lst = new List<KeyValuePair<string, string>>();
  502. foreach (var item in this.Source)
  503. {
  504. lst.Add(new KeyValuePair<string, string>(item.Key, item.Value));
  505. }
  506. ucTime.Source = lst;
  507. ucTime.SetSelect(_selectedValue);
  508. _frmAnchor = new Forms.FrmAnchor(this, ucTime);
  509. _frmAnchor.Load += (a, b) => { (a as Form).Size = size; };
  510. _frmAnchor.Show(this.FindForm());
  511. }
  512. }
  513. else
  514. {
  515. _frmAnchor.Close();
  516. }
  517. }
  518. /// <summary>
  519. /// The FRM anchor
  520. /// </summary>
  521. Forms.FrmAnchor _frmAnchor;
  522. /// <summary>
  523. /// Handles the SelectSourceEvent event of the ucTime control.
  524. /// </summary>
  525. /// <param name="sender">The source of the event.</param>
  526. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  527. void ucTime_SelectSourceEvent(object sender, EventArgs e)
  528. {
  529. if (_frmAnchor != null && !_frmAnchor.IsDisposed && _frmAnchor.Visible)
  530. {
  531. SelectedValue = sender.ToString();
  532. _frmAnchor.Close();
  533. }
  534. }
  535. /// <summary>
  536. /// Handles the Load event of the UCComboBox control.
  537. /// </summary>
  538. /// <param name="sender">The source of the event.</param>
  539. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  540. private void UCComboBox_Load(object sender, EventArgs e)
  541. {
  542. if (this._BoxStyle == ComboBoxStyle.DropDownList)
  543. {
  544. txtInput.BackColor = _BackColor;
  545. base.FillColor = _BackColor;
  546. base.RectColor = _BackColor;
  547. }
  548. else
  549. {
  550. txtInput.BackColor = Color.White;
  551. base.FillColor = Color.White;
  552. base.RectColor = Color.FromArgb(220, 220, 220);
  553. }
  554. }
  555. }
  556. }