// *********************************************************************** // Assembly : HZH_Controls // Created : 08-28-2019 // // *********************************************************************** // // Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com // // // Blog: https://www.cnblogs.com/bfyx // GitHub:https://github.com/kwwwvagaa/NetWinformControl // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git // // If you use this code, please keep this note. // *********************************************************************** using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; using HZH_Controls.Controls; namespace HZH_Controls.Controls { /// /// Class UCComboxGrid. /// Implements the /// /// public partial class UCComboxGrid : UCCombox { /// /// The m row type /// private Type m_rowType = typeof(UCDataGridViewRow); /// /// 表格行类型 /// /// The type of the grid row. [Description("表格行类型"), Category("自定义")] public Type GridRowType { get { return m_rowType; } set { m_rowType = value; } } /// /// The int width /// int intWidth = 0; /// /// The m columns /// private List m_columns = null; /// /// 表格列 /// /// The grid columns. [Description("表格列"), Category("自定义")] public List GridColumns { get { return m_columns; } set { m_columns = value; if (value != null) intWidth = value.Sum(p => p.WidthType == SizeType.Absolute ? p.Width : (p.Width < 80 ? 80 : p.Width)); } } /// /// The m data source /// private List m_dataSource = null; /// /// 表格数据源 /// /// The grid data source. [Description("表格数据源"), Category("自定义")] public List GridDataSource { get { return m_dataSource; } set { m_dataSource = value; } } /// /// The m text field /// private string m_textField; /// /// 显示值字段名称 /// /// The text field. [Description("显示值字段名称"), Category("自定义")] public string TextField { get { return m_textField; } set { m_textField = value; SetText(); } } /// /// 控件样式 /// /// The box style. [Obsolete("不再可用的属性")] [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] private new ComboBoxStyle BoxStyle { get; set; } /// /// The select source /// private object selectSource = null; /// /// 选中的数据源 /// /// The select source. [Description("选中的数据源"), Category("自定义")] public object SelectSource { get { return selectSource; } set { selectSource = value; SetText(); if (SelectedChangedEvent != null) { SelectedChangedEvent(value, null); } } } /// /// 选中数据源改变事件 /// [Description("选中数据源改变事件"), Category("自定义")] public new event EventHandler SelectedChangedEvent; /// /// Initializes a new instance of the class. /// public UCComboxGrid() { InitializeComponent(); } /// /// The m uc panel /// UCComboxGridPanel m_ucPanel = null; /// /// The FRM anchor /// Forms.FrmAnchor _frmAnchor; /// /// Handles the MouseDown event of the click control. /// /// The source of the event. /// The instance containing the event data. protected override void click_MouseDown(object sender, MouseEventArgs e) { if (m_columns == null || m_columns.Count <= 0) return; if (m_ucPanel == null) { var p = this.Parent.PointToScreen(this.Location); int intScreenHeight = Screen.PrimaryScreen.Bounds.Height; int intHeight = Math.Max(p.Y, intScreenHeight - p.Y - this.Height); intHeight -= 100; m_ucPanel = new UCComboxGridPanel(); m_ucPanel.ItemClick += m_ucPanel_ItemClick; m_ucPanel.Height = intHeight; m_ucPanel.Width = intWidth; m_ucPanel.Columns = m_columns; m_ucPanel.RowType = m_rowType; if (m_dataSource != null && m_dataSource.Count > 0) { int _intHeight = Math.Min(110 + m_dataSource.Count * 36, m_ucPanel.Height); if (_intHeight <= 0) _intHeight = 100; m_ucPanel.Height = _intHeight; } } m_ucPanel.DataSource = m_dataSource; if (_frmAnchor == null || _frmAnchor.IsDisposed || _frmAnchor.Visible == false) { _frmAnchor = new Forms.FrmAnchor(this, m_ucPanel, isNotFocus: false); _frmAnchor.Show(this.FindForm()); } } /// /// Handles the ItemClick event of the m_ucPanel control. /// /// The source of the event. /// The instance containing the event data. void m_ucPanel_ItemClick(object sender, DataGridViewEventArgs e) { _frmAnchor.Hide(); SelectSource = sender; } /// /// Sets the text. /// private void SetText() { if (!string.IsNullOrEmpty(m_textField) && selectSource != null) { var pro = selectSource.GetType().GetProperty(m_textField); if (pro != null) { TextValue = pro.GetValue(selectSource, null).ToStringExt(); } } } } }