UCHorizontalListItem.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 08-08-2019
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCHorizontalListItem.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. namespace HZH_Controls.Controls
  25. {
  26. /// <summary>
  27. /// Class UCHorizontalListItem.
  28. /// Implements the <see cref="System.Windows.Forms.UserControl" />
  29. /// </summary>
  30. /// <seealso cref="System.Windows.Forms.UserControl" />
  31. [ToolboxItem(false)]
  32. public partial class UCHorizontalListItem : UserControl
  33. {
  34. /// <summary>
  35. /// Occurs when [selected item].
  36. /// </summary>
  37. public event EventHandler SelectedItem;
  38. /// <summary>
  39. /// The data source
  40. /// </summary>
  41. private KeyValuePair<string, string> _DataSource = new KeyValuePair<string, string>();
  42. /// <summary>
  43. /// Gets or sets the data source.
  44. /// </summary>
  45. /// <value>The data source.</value>
  46. public KeyValuePair<string, string> DataSource
  47. {
  48. get { return _DataSource; }
  49. set
  50. {
  51. _DataSource = value;
  52. var g = lblTitle.CreateGraphics();
  53. int intWidth = ControlHelper.GetStringWidth(value.Value, g, lblTitle.Font);
  54. g.Dispose();
  55. if (intWidth < 50)
  56. intWidth = 50;
  57. this.Width = intWidth + 20;
  58. lblTitle.Text = value.Value;
  59. SetSelect(false);
  60. }
  61. }
  62. private Color selectedColor = Color.FromArgb(255, 77, 59);
  63. public Color SelectedColor
  64. {
  65. get { return selectedColor; }
  66. set { selectedColor = value; }
  67. }
  68. /// <summary>
  69. /// Initializes a new instance of the <see cref="UCHorizontalListItem" /> class.
  70. /// </summary>
  71. public UCHorizontalListItem()
  72. {
  73. InitializeComponent();
  74. this.Dock = DockStyle.Right;
  75. this.MouseDown += Item_MouseDown;
  76. this.lblTitle.MouseDown += Item_MouseDown;
  77. this.ucSplitLine_H1.MouseDown += Item_MouseDown;
  78. }
  79. /// <summary>
  80. /// Handles the MouseDown event of the Item control.
  81. /// </summary>
  82. /// <param name="sender">The source of the event.</param>
  83. /// <param name="e">The <see cref="MouseEventArgs" /> instance containing the event data.</param>
  84. void Item_MouseDown(object sender, MouseEventArgs e)
  85. {
  86. if (SelectedItem != null)
  87. SelectedItem(this, e);
  88. }
  89. /// <summary>
  90. /// Sets the select.
  91. /// </summary>
  92. /// <param name="bln">if set to <c>true</c> [BLN].</param>
  93. public void SetSelect(bool bln)
  94. {
  95. if (bln)
  96. {
  97. lblTitle.ForeColor = selectedColor;
  98. ucSplitLine_H1.BackColor = selectedColor;
  99. ucSplitLine_H1.Visible = true;
  100. this.lblTitle.Padding = new Padding(0, 0, 0, 5);
  101. }
  102. else
  103. {
  104. lblTitle.ForeColor = Color.FromArgb(64, 64, 64);
  105. ucSplitLine_H1.Visible = false;
  106. this.lblTitle.Padding = new Padding(0, 0, 0, 0);
  107. }
  108. }
  109. }
  110. }