// ***********************************************************************
// Assembly : HZH_Controls
// Created : 08-08-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 System.Drawing.Drawing2D;
namespace HZH_Controls.Controls
{
///
/// Class UCCombox.
/// Implements the
///
///
[DefaultEvent("SelectedChangedEvent")]
public partial class UCCombox : UCControlBase
{
///
/// The fore color
///
Color _ForeColor = Color.FromArgb(64, 64, 64);
///
/// 文字颜色
///
/// The color of the fore.
///
///
///
[Description("文字颜色"), Category("自定义")]
public override Color ForeColor
{
get
{
return _ForeColor;
}
set
{
_ForeColor = value;
lblInput.ForeColor = value;
txtInput.ForeColor = value;
}
}
Color _selectItemColor = Color.FromArgb(64, 64, 64);
[Description("选中颜色"), Category("自定义")]
public Color SelectItemColor
{
get
{
return _selectItemColor;
}
set
{
_selectItemColor = value;
}
}
///
/// 选中事件
///
[Description("选中事件"), Category("自定义")]
public event EventHandler SelectedChangedEvent;
///
/// 文本改变事件
///
[Description("文本改变事件"), Category("自定义")]
public event EventHandler TextChangedEvent;
///
/// The box style
///
private ComboBoxStyle _BoxStyle = ComboBoxStyle.DropDown;
///
/// 控件样式
///
/// The box style.
[Description("控件样式"), Category("自定义")]
public ComboBoxStyle BoxStyle
{
get { return _BoxStyle; }
set
{
_BoxStyle = value;
if (value == ComboBoxStyle.DropDownList)
{
lblInput.Visible = true;
txtInput.Visible = false;
}
else
{
lblInput.Visible = false;
txtInput.Visible = true;
}
if (this._BoxStyle == ComboBoxStyle.DropDownList)
{
txtInput.BackColor = _BackColor;
base.FillColor = _BackColor;
base.RectColor = _BackColor;
}
else
{
txtInput.BackColor = Color.White;
base.FillColor = Color.White;
base.RectColor = Color.FromArgb(220, 220, 220);
}
}
}
///
/// The font
///
private Font _Font = new Font("微软雅黑", 12);
///
/// 字体
///
/// The font.
///
///
///
///
///
///
[Description("字体"), Category("自定义")]
public new Font Font
{
get { return _Font; }
set
{
_Font = value;
lblInput.Font = value;
txtInput.Font = value;
txtInput.PromptFont = value;
this.txtInput.Location = new Point(this.txtInput.Location.X, (this.Height - txtInput.Height) / 2);
this.lblInput.Location = new Point(this.lblInput.Location.X, (this.Height - lblInput.Height) / 2);
}
}
///
/// 当使用边框时填充颜色,当值为背景色或透明色或空值则不填充
///
/// The color of the fill.
[Obsolete("不再可用的属性")]
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
private new Color FillColor
{
get;
set;
}
///
/// 边框颜色
///
/// The color of the rect.
[Obsolete("不再可用的属性")]
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
private new Color RectColor
{
get;
set;
}
///
/// The text value
///
private string _TextValue;
///
/// 文字
///
/// The text value.
[Description("文字"), Category("自定义")]
public string TextValue
{
get { return _TextValue; }
set
{
_TextValue = value;
if (lblInput.Text != value)
lblInput.Text = value;
if (txtInput.Text != value)
txtInput.Text = value;
}
}
///
/// The source
///
private List> _source = null;
///
/// 数据源
///
/// The source.
[Description("数据源"), Category("自定义")]
public List> Source
{
get { return _source; }
set
{
_source = value;
_selectedIndex = -1;
_selectedValue = "";
_selectedItem = new KeyValuePair();
_selectedText = "";
lblInput.Text = "";
txtInput.Text = "";
}
}
///
/// The selected item
///
private KeyValuePair _selectedItem = new KeyValuePair();
///
/// The selected index
///
private int _selectedIndex = -1;
///
/// 选中的数据下标
///
/// The index of the selected.
[Description("选中的数据下标"), Category("自定义")]
public int SelectedIndex
{
get
{
return _selectedIndex;
}
set
{
if (value < 0 || _source == null || _source.Count <= 0 || value >= _source.Count)
{
_selectedIndex = -1;
_selectedValue = "";
_selectedItem = new KeyValuePair();
SelectedText = "";
}
else
{
_selectedIndex = value;
_selectedItem = _source[value];
_selectedValue = _source[value].Key;
SelectedText = _source[value].Value;
}
}
}
///
/// The selected value
///
private string _selectedValue = "";
///
/// 选中的值
///
/// The selected value.
[Description("选中的值"), Category("自定义")]
public string SelectedValue
{
get
{
return _selectedValue;
}
set
{
if (_source == null || _source.Count <= 0)
{
SelectedText = "";
_selectedValue = "";
_selectedIndex = -1;
_selectedItem = new KeyValuePair();
}
else
{
for (int i = 0; i < _source.Count; i++)
{
if (_source[i].Key == value)
{
_selectedValue = value;
_selectedIndex = i;
_selectedItem = _source[i];
SelectedText = _source[i].Value;
return;
}
}
_selectedValue = "";
_selectedIndex = -1;
_selectedItem = new KeyValuePair();
SelectedText = "";
}
}
}
///
/// The selected text
///
private string _selectedText = "";
///
/// 选中的文本
///
/// The selected text.
[Description("选中的文本"), Category("自定义")]
public string SelectedText
{
get { return _selectedText; }
private set
{
_selectedText = value;
lblInput.Text = _selectedText;
txtInput.Text = _selectedText;
if (SelectedChangedEvent != null)
{
SelectedChangedEvent(this, null);
}
}
}
///
/// The item width
///
private int _ItemWidth = 70;
///
/// 项宽度
///
/// The width of the item.
[Description("项宽度"), Category("自定义")]
public int ItemWidth
{
get { return _ItemWidth; }
set { _ItemWidth = value; }
}
///
/// The drop panel height
///
private int _dropPanelHeight = -1;
///
/// 下拉面板高度
///
/// The height of the drop panel.
[Description("下拉面板高度"), Category("自定义")]
public int DropPanelHeight
{
get { return _dropPanelHeight; }
set { _dropPanelHeight = value; }
}
///
/// 获取或设置控件的背景色。
///
/// The color of the back.
///
///
///
[Obsolete("不再可用的属性,如需要改变背景色,请使用BackColorExt")]
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
private new Color BackColor
{
get
{
return base.BackColor;
}
set
{
base.BackColor = Color.Transparent;
}
}
///
/// The back color
///
private Color _BackColor = Color.FromArgb(240, 240, 240);
///
/// 背景色
///
/// The back color ext.
[Description("背景色"), Category("自定义")]
public Color BackColorExt
{
get
{
return _BackColor;
}
set
{
if (value == Color.Transparent)
return;
_BackColor = value;
lblInput.BackColor = value;
if (this._BoxStyle == ComboBoxStyle.DropDownList)
{
txtInput.BackColor = value;
base.FillColor = value;
base.RectColor = value;
}
else
{
txtInput.BackColor = Color.White;
base.FillColor = Color.White;
base.RectColor = Color.FromArgb(220, 220, 220);
}
}
}
///
/// The triangle color
///
private Color triangleColor = Color.FromArgb(255, 77, 59);
///
/// 三角颜色
///
/// The color of the triangle.
[Description("三角颜色"), Category("自定义")]
public Color TriangleColor
{
get { return triangleColor; }
set
{
triangleColor = value;
Bitmap bit = new Bitmap(12, 10);
Graphics g = Graphics.FromImage(bit);
g.SetGDIHigh();
GraphicsPath path = new GraphicsPath();
path.AddLines(new Point[]
{
new Point(1,1),
new Point(11,1),
new Point(6,10),
new Point(1,1)
});
g.FillPath(new SolidBrush(value), path);
g.Dispose();
panel1.BackgroundImage = bit;
}
}
///
/// Initializes a new instance of the class.
///
public UCCombox()
{
InitializeComponent();
lblInput.BackColor = _BackColor;
if (this._BoxStyle == ComboBoxStyle.DropDownList)
{
txtInput.BackColor = _BackColor;
base.FillColor = _BackColor;
base.RectColor = _BackColor;
}
else
{
txtInput.BackColor = Color.White;
base.FillColor = Color.White;
base.RectColor = Color.FromArgb(220, 220, 220);
}
base.BackColor = Color.Transparent;
}
///
/// Handles the SizeChanged event of the UCComboBox control.
///
/// The source of the event.
/// The instance containing the event data.
private void UCComboBox_SizeChanged(object sender, EventArgs e)
{
this.txtInput.Location = new Point(this.txtInput.Location.X, (this.Height - txtInput.Height) / 2);
this.lblInput.Location = new Point(this.lblInput.Location.X, (this.Height - lblInput.Height) / 2);
}
///
/// Handles the TextChanged event of the txtInput control.
///
/// The source of the event.
/// The instance containing the event data.
private void txtInput_TextChanged(object sender, EventArgs e)
{
TextValue = txtInput.Text;
if (TextChangedEvent != null)
{
TextChangedEvent(this, null);
}
}
///
/// Handles the MouseDown event of the click control.
///
/// The source of the event.
/// The instance containing the event data.
protected virtual void click_MouseDown(object sender, MouseEventArgs e)
{
if (_frmAnchor == null || _frmAnchor.IsDisposed || _frmAnchor.Visible == false)
{
if (this.Source != null && this.Source.Count > 0)
{
int intRow = 0;
int intCom = 1;
var p = this.PointToScreen(this.Location);
while (true)
{
int intScreenHeight = Screen.PrimaryScreen.Bounds.Height;
if ((p.Y + this.Height + this.Source.Count / intCom * 50 < intScreenHeight || p.Y - this.Source.Count / intCom * 50 > 0)
&& (_dropPanelHeight <= 0 ? true : (this.Source.Count / intCom * 50 <= _dropPanelHeight)))
{
intRow = this.Source.Count / intCom + (this.Source.Count % intCom != 0 ? 1 : 0);
break;
}
intCom++;
}
UCTimePanel ucTime = new UCTimePanel();
ucTime.SelectColor = SelectItemColor;
ucTime.IsShowBorder = true;
int intWidth = this.Width / intCom;
if (intWidth < _ItemWidth)
intWidth = _ItemWidth;
Size size = new Size(intCom * intWidth, intRow * 50);
ucTime.Size = size;
ucTime.FirstEvent = true;
ucTime.SelectSourceEvent += ucTime_SelectSourceEvent;
ucTime.Row = intRow;
ucTime.Column = intCom;
List> lst = new List>();
foreach (var item in this.Source)
{
lst.Add(new KeyValuePair(item.Key, item.Value));
}
ucTime.Source = lst;
ucTime.SetSelect(_selectedValue);
_frmAnchor = new Forms.FrmAnchor(this, ucTime);
_frmAnchor.Load += (a, b) => { (a as Form).Size = size; };
_frmAnchor.Show(this.FindForm());
}
}
else
{
_frmAnchor.Close();
}
}
///
/// The FRM anchor
///
Forms.FrmAnchor _frmAnchor;
///
/// Handles the SelectSourceEvent event of the ucTime control.
///
/// The source of the event.
/// The instance containing the event data.
void ucTime_SelectSourceEvent(object sender, EventArgs e)
{
if (_frmAnchor != null && !_frmAnchor.IsDisposed && _frmAnchor.Visible)
{
SelectedValue = sender.ToString();
_frmAnchor.Close();
}
}
///
/// Handles the Load event of the UCComboBox control.
///
/// The source of the event.
/// The instance containing the event data.
private void UCComboBox_Load(object sender, EventArgs e)
{
txtInput.ForeColor = ForeColor;
if (this._BoxStyle == ComboBoxStyle.DropDownList)
{
txtInput.BackColor = _BackColor;
base.FillColor = _BackColor;
base.RectColor = _BackColor;
}
else
{
txtInput.BackColor = Color.White;
base.FillColor = Color.White;
base.RectColor = Color.FromArgb(220, 220, 220);
}
}
}
}