UCComboxGrid.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 08-28-2019
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCComboxGrid.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 HZH_Controls.Controls;
  25. namespace HZH_Controls.Controls
  26. {
  27. /// <summary>
  28. /// Class UCComboxGrid.
  29. /// Implements the <see cref="HZH_Controls.Controls.UCCombox" />
  30. /// </summary>
  31. /// <seealso cref="HZH_Controls.Controls.UCCombox" />
  32. public partial class UCComboxGrid : UCCombox
  33. {
  34. /// <summary>
  35. /// The m row type
  36. /// </summary>
  37. private Type m_rowType = typeof(UCDataGridViewRow);
  38. /// <summary>
  39. /// 表格行类型
  40. /// </summary>
  41. /// <value>The type of the grid row.</value>
  42. [Description("表格行类型"), Category("自定义")]
  43. public Type GridRowType
  44. {
  45. get { return m_rowType; }
  46. set
  47. {
  48. m_rowType = value;
  49. }
  50. }
  51. /// <summary>
  52. /// The int width
  53. /// </summary>
  54. int intWidth = 0;
  55. /// <summary>
  56. /// The m columns
  57. /// </summary>
  58. private List<DataGridViewColumnEntity> m_columns = null;
  59. /// <summary>
  60. /// 表格列
  61. /// </summary>
  62. /// <value>The grid columns.</value>
  63. [Description("表格列"), Category("自定义")]
  64. public List<DataGridViewColumnEntity> GridColumns
  65. {
  66. get { return m_columns; }
  67. set
  68. {
  69. m_columns = value;
  70. if (value != null)
  71. intWidth = value.Sum(p => p.WidthType == SizeType.Absolute ? p.Width : (p.Width < 80 ? 80 : p.Width));
  72. }
  73. }
  74. /// <summary>
  75. /// The m data source
  76. /// </summary>
  77. private List<object> m_dataSource = null;
  78. /// <summary>
  79. /// 表格数据源
  80. /// </summary>
  81. /// <value>The grid data source.</value>
  82. [Description("表格数据源"), Category("自定义")]
  83. public List<object> GridDataSource
  84. {
  85. get { return m_dataSource; }
  86. set { m_dataSource = value; }
  87. }
  88. /// <summary>
  89. /// The m text field
  90. /// </summary>
  91. private string m_textField;
  92. /// <summary>
  93. /// 显示值字段名称
  94. /// </summary>
  95. /// <value>The text field.</value>
  96. [Description("显示值字段名称"), Category("自定义")]
  97. public string TextField
  98. {
  99. get { return m_textField; }
  100. set
  101. {
  102. m_textField = value;
  103. SetText();
  104. }
  105. }
  106. /// <summary>
  107. /// 控件样式
  108. /// </summary>
  109. /// <value>The box style.</value>
  110. [Obsolete("不再可用的属性")]
  111. [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
  112. private new ComboBoxStyle BoxStyle
  113. {
  114. get;
  115. set;
  116. }
  117. /// <summary>
  118. /// The select source
  119. /// </summary>
  120. private object selectSource = null;
  121. /// <summary>
  122. /// 选中的数据源
  123. /// </summary>
  124. /// <value>The select source.</value>
  125. [Description("选中的数据源"), Category("自定义")]
  126. public object SelectSource
  127. {
  128. get { return selectSource; }
  129. set
  130. {
  131. selectSource = value;
  132. SetText();
  133. if (SelectedChangedEvent != null)
  134. {
  135. SelectedChangedEvent(value, null);
  136. }
  137. }
  138. }
  139. /// <summary>
  140. /// 选中数据源改变事件
  141. /// </summary>
  142. [Description("选中数据源改变事件"), Category("自定义")]
  143. public new event EventHandler SelectedChangedEvent;
  144. /// <summary>
  145. /// Initializes a new instance of the <see cref="UCComboxGrid" /> class.
  146. /// </summary>
  147. public UCComboxGrid()
  148. {
  149. InitializeComponent();
  150. }
  151. /// <summary>
  152. /// The m uc panel
  153. /// </summary>
  154. UCComboxGridPanel m_ucPanel = null;
  155. /// <summary>
  156. /// The FRM anchor
  157. /// </summary>
  158. Forms.FrmAnchor _frmAnchor;
  159. /// <summary>
  160. /// Handles the MouseDown event of the click control.
  161. /// </summary>
  162. /// <param name="sender">The source of the event.</param>
  163. /// <param name="e">The <see cref="MouseEventArgs" /> instance containing the event data.</param>
  164. protected override void click_MouseDown(object sender, MouseEventArgs e)
  165. {
  166. if (m_columns == null || m_columns.Count <= 0)
  167. return;
  168. if (m_ucPanel == null)
  169. {
  170. var p = this.Parent.PointToScreen(this.Location);
  171. int intScreenHeight = Screen.PrimaryScreen.Bounds.Height;
  172. int intHeight = Math.Max(p.Y, intScreenHeight - p.Y - this.Height);
  173. intHeight -= 100;
  174. m_ucPanel = new UCComboxGridPanel();
  175. m_ucPanel.ItemClick += m_ucPanel_ItemClick;
  176. m_ucPanel.Height = intHeight;
  177. m_ucPanel.Width = intWidth;
  178. m_ucPanel.Columns = m_columns;
  179. m_ucPanel.RowType = m_rowType;
  180. if (m_dataSource != null && m_dataSource.Count > 0)
  181. {
  182. int _intHeight = Math.Min(110 + m_dataSource.Count * 36, m_ucPanel.Height);
  183. if (_intHeight <= 0)
  184. _intHeight = 100;
  185. m_ucPanel.Height = _intHeight;
  186. }
  187. }
  188. m_ucPanel.DataSource = m_dataSource;
  189. if (_frmAnchor == null || _frmAnchor.IsDisposed || _frmAnchor.Visible == false)
  190. {
  191. _frmAnchor = new Forms.FrmAnchor(this, m_ucPanel, isNotFocus: false);
  192. _frmAnchor.Show(this.FindForm());
  193. }
  194. }
  195. /// <summary>
  196. /// Handles the ItemClick event of the m_ucPanel control.
  197. /// </summary>
  198. /// <param name="sender">The source of the event.</param>
  199. /// <param name="e">The <see cref="DataGridViewEventArgs" /> instance containing the event data.</param>
  200. void m_ucPanel_ItemClick(object sender, DataGridViewEventArgs e)
  201. {
  202. _frmAnchor.Hide();
  203. SelectSource = sender;
  204. }
  205. /// <summary>
  206. /// Sets the text.
  207. /// </summary>
  208. private void SetText()
  209. {
  210. if (!string.IsNullOrEmpty(m_textField) && selectSource != null)
  211. {
  212. var pro = selectSource.GetType().GetProperty(m_textField);
  213. if (pro != null)
  214. {
  215. TextValue = pro.GetValue(selectSource, null).ToStringExt();
  216. }
  217. }
  218. }
  219. }
  220. }