SunnyUI/SunnyUI/Controls/UIComboBox.cs

374 lines
12 KiB
C#
Raw Normal View History

2020-05-11 21:11:29 +08:00
/******************************************************************************
* SunnyUI
2022-01-02 12:32:50 +08:00
* CopyRight (C) 2012-2022 ShenYongHua().
2021-02-20 15:45:47 +08:00
* QQ群56829229 QQ17612584 EMailSunnyUI@QQ.Com
2020-05-11 21:11:29 +08:00
*
* Blog: https://www.cnblogs.com/yhuse
* Gitee: https://gitee.com/yhuse/SunnyUI
* GitHub: https://github.com/yhuse/SunnyUI
*
* SunnyUI.dll can be used for free under the GPL-3.0 license.
* If you use this code, please keep this note.
* 使
******************************************************************************
* : UIComboBox.cs
* :
2022-01-05 21:57:47 +08:00
* : V3.1
2020-05-11 21:11:29 +08:00
* : 2020-01-01
*
* 2020-01-01: V2.2.0
* 2020-06-11: V2.2.5 DataSource
* 2021-05-06: V3.0.3 SelectedIndexChanged两次的问题
* 2021-06-03: V3.0.4
* 2021-08-03: V3.0.5 Items.Clear后清除显示
* 2021-08-15: V3.0.6
* 2022-01-16: V3.1.0
2020-05-11 21:11:29 +08:00
******************************************************************************/
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;
namespace Sunny.UI
{
[DefaultProperty("Items")]
[DefaultEvent("SelectedIndexChanged")]
[ToolboxItem(true)]
2020-06-11 23:53:51 +08:00
[LookupBindingProperties("DataSource", "DisplayMember", "ValueMember", "SelectedValue")]
2021-08-23 10:51:09 +08:00
public sealed partial class UIComboBox : UIDropControl, IToolTip
2020-05-11 21:11:29 +08:00
{
public UIComboBox()
{
InitializeComponent();
ListBox.SelectedIndexChanged += Box_SelectedIndexChanged;
ListBox.DataSourceChanged += Box_DataSourceChanged;
ListBox.DisplayMemberChanged += Box_DisplayMemberChanged;
ListBox.ValueMemberChanged += Box_ValueMemberChanged;
ListBox.SelectedValueChanged += ListBox_SelectedValueChanged;
ListBox.ItemsClear += ListBox_ItemsClear;
ListBox.ItemsRemove += ListBox_ItemsRemove;
2021-06-15 11:53:27 +08:00
edit.TextChanged += Edit_TextChanged;
DropDownWidth = 150;
fullControlSelect = true;
2020-06-11 23:53:51 +08:00
}
public Control ExToolTipControl()
{
return edit;
}
2021-08-05 14:09:32 +08:00
[DefaultValue(false)]
public bool Sorted
{
get => ListBox.Sorted;
set => ListBox.Sorted = value;
}
2021-08-05 13:18:15 +08:00
public int FindString(string s)
{
return ListBox.FindString(s);
}
2021-08-05 14:09:32 +08:00
public int FindString(string s, int startIndex)
{
return ListBox.FindString(s, startIndex);
}
2021-08-05 13:18:15 +08:00
public int FindStringExact(string s)
{
return ListBox.FindStringExact(s);
}
2021-08-05 14:09:32 +08:00
public int FindStringExact(string s, int startIndex)
{
return ListBox.FindStringExact(s, startIndex);
}
private void ListBox_ItemsRemove(object sender, EventArgs e)
{
if (ListBox.Count == 0)
{
Text = "";
edit.Text = "";
}
}
private void ListBox_ItemsClear(object sender, EventArgs e)
{
Text = "";
edit.Text = "";
}
public new event EventHandler TextChanged;
2021-06-15 11:53:27 +08:00
private void Edit_TextChanged(object sender, EventArgs e)
{
TextChanged?.Invoke(this, e);
}
private void ListBox_SelectedValueChanged(object sender, EventArgs e)
{
SelectedValueChanged?.Invoke(this, e);
}
2020-06-11 23:53:51 +08:00
private void Box_ValueMemberChanged(object sender, EventArgs e)
{
ValueMemberChanged?.Invoke(this, e);
2020-06-11 23:53:51 +08:00
}
private void Box_DisplayMemberChanged(object sender, EventArgs e)
{
DisplayMemberChanged?.Invoke(this, e);
2020-06-11 23:53:51 +08:00
}
private void Box_DataSourceChanged(object sender, EventArgs e)
{
DataSourceChanged?.Invoke(this, e);
2020-06-11 23:53:51 +08:00
}
private void Box_SelectedIndexChanged(object sender, EventArgs e)
{
Text = ListBox.GetItemText(ListBox.SelectedItem);
SelectedIndexChanged?.Invoke(this, e);
2020-06-11 23:53:51 +08:00
}
2020-05-11 21:11:29 +08:00
public event EventHandler SelectedIndexChanged;
2020-06-11 23:53:51 +08:00
public event EventHandler DataSourceChanged;
public event EventHandler DisplayMemberChanged;
public event EventHandler ValueMemberChanged;
public event EventHandler SelectedValueChanged;
2020-05-11 21:11:29 +08:00
protected override void ItemForm_ValueChanged(object sender, object value)
{
2021-06-07 21:58:36 +08:00
//if (SelectedIndex != ListBox.SelectedIndex)
//{
// SelectedIndex = ListBox.SelectedIndex;
// //Box_SelectedIndexChanged(null, null);
// Invalidate();
//}
Invalidate();
2020-05-11 21:11:29 +08:00
}
private readonly UIComboBoxItem dropForm = new UIComboBoxItem();
2020-05-11 21:11:29 +08:00
protected override void CreateInstance()
{
ItemForm = new UIDropDown(dropForm);
2020-05-11 21:11:29 +08:00
}
protected override int CalcItemFormHeight()
{
int interval = ItemForm.Height - ItemForm.ClientRectangle.Height;
return 4 + Math.Min(ListBox.Items.Count, MaxDropDownItems) * ItemHeight + interval;
2020-05-11 21:11:29 +08:00
}
private UIListBox ListBox
{
get => dropForm.ListBox;
2020-05-11 21:11:29 +08:00
}
[DefaultValue(25)]
[Description("列表项高度"), Category("SunnyUI")]
2020-05-11 21:11:29 +08:00
public int ItemHeight
{
get => ListBox.ItemHeight;
set => ListBox.ItemHeight = value;
}
[DefaultValue(8)]
[Description("列表下拉最大个数"), Category("SunnyUI")]
2020-05-11 21:11:29 +08:00
public int MaxDropDownItems { get; set; } = 8;
2020-06-11 23:53:51 +08:00
private void UIComboBox_FontChanged(object sender, EventArgs e)
2020-05-11 21:11:29 +08:00
{
2020-06-11 23:53:51 +08:00
if (ItemForm != null)
2020-05-11 21:11:29 +08:00
{
2020-06-11 23:53:51 +08:00
ListBox.Font = Font;
2020-05-11 21:11:29 +08:00
}
}
2021-08-23 10:51:09 +08:00
public void ShowDropDown()
{
UIComboBox_ButtonClick(this, EventArgs.Empty);
}
2020-05-11 21:11:29 +08:00
private void UIComboBox_ButtonClick(object sender, EventArgs e)
{
if (Items.Count > 0)
ItemForm.Show(this, new Size(DropDownWidth < Width ? Width : DropDownWidth, CalcItemFormHeight()));
2020-05-11 21:11:29 +08:00
}
public override void SetStyleColor(UIBaseStyle uiColor)
{
base.SetStyleColor(uiColor);
ListBox.SetStyleColor(uiColor);
}
2020-06-11 23:53:51 +08:00
public object DataSource
{
get => ListBox.DataSource;
set => ListBox.DataSource = value;
2020-06-11 23:53:51 +08:00
}
[DefaultValue(150)]
[Description("下拉框宽度"), Category("SunnyUI")]
public int DropDownWidth { get; set; }
2020-06-11 23:53:51 +08:00
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Localizable(true)]
[Editor("System.Windows.Forms.Design.ListControlStringCollectionEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
2020-06-11 23:53:51 +08:00
[MergableProperty(false)]
[Description("列表项"), Category("SunnyUI")]
public ListBox.ObjectCollection Items => ListBox.Items;
2020-06-11 23:53:51 +08:00
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[Description("选中索引"), Category("SunnyUI")]
2020-06-11 23:53:51 +08:00
public int SelectedIndex
{
get => ListBox.SelectedIndex;
set => ListBox.SelectedIndex = value;
2020-06-11 23:53:51 +08:00
}
[Browsable(false), Bindable(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[Description("选中项"), Category("SunnyUI")]
2020-06-11 23:53:51 +08:00
public object SelectedItem
{
get => ListBox.SelectedItem;
set => ListBox.SelectedItem = value;
2020-06-11 23:53:51 +08:00
}
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[Description("选中文字"), Category("SunnyUI")]
2020-06-11 23:53:51 +08:00
public string SelectedText
{
get
{
if (DropDownStyle == UIDropDownStyle.DropDown)
{
return edit.SelectedText;
}
else
{
return Text;
}
}
2020-06-11 23:53:51 +08:00
}
public override void ResetText()
{
Clear();
2020-06-11 23:53:51 +08:00
}
[Description("获取或设置要为此列表框显示的属性。"), Category("SunnyUI")]
2020-06-11 23:53:51 +08:00
[DefaultValue("")]
[Editor("System.Windows.Forms.Design.DataMemberFieldEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
[TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public string DisplayMember
{
get => ListBox.DisplayMember;
set => ListBox.DisplayMember = value;
2020-06-11 23:53:51 +08:00
}
[Description("获取或设置指示显示值的方式的格式说明符字符。"), Category("SunnyUI")]
2020-06-11 23:53:51 +08:00
[DefaultValue("")]
[Editor("System.Windows.Forms.Design.FormatStringEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
[MergableProperty(false)]
public string FormatString
{
get => ListBox.FormatString;
set => ListBox.FormatString = value;
2020-06-11 23:53:51 +08:00
}
[Description("获取或设置指示显示值是否可以进行格式化操作。"), Category("SunnyUI")]
2020-06-11 23:53:51 +08:00
[DefaultValue(false)]
public bool FormattingEnabled
{
get => ListBox.FormattingEnabled;
set => ListBox.FormattingEnabled = value;
2020-06-11 23:53:51 +08:00
}
[Description("获取或设置要为此列表框实际值的属性。"), Category("SunnyUI")]
2020-06-11 23:53:51 +08:00
[DefaultValue("")]
[Editor("System.Windows.Forms.Design.DataMemberFieldEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
public string ValueMember
{
get => ListBox.ValueMember;
set => ListBox.ValueMember = value;
}
2020-06-11 23:53:51 +08:00
[
DefaultValue(null),
Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
Bindable(true)
]
2020-06-11 23:53:51 +08:00
public object SelectedValue
{
get => ListBox.SelectedValue;
set => ListBox.SelectedValue = value;
2020-06-11 23:53:51 +08:00
}
public string GetItemText(object item)
{
return ListBox.GetItemText(item);
2020-06-11 23:53:51 +08:00
}
2021-08-23 10:51:09 +08:00
private void UIComboBox_KeyDown(object sender, KeyEventArgs e)
{
2021-10-16 22:33:10 +08:00
if (e.KeyCode == Keys.Enter)
2021-08-23 10:51:09 +08:00
{
ShowDropDown();
}
}
private void edit_KeyDown(object sender, KeyEventArgs e)
{
2021-10-16 22:33:10 +08:00
if (e.KeyCode == Keys.Enter)
2021-08-23 10:51:09 +08:00
{
ShowDropDown();
}
}
[DefaultValue(typeof(Color), "White")]
public Color ItemFillColor
{
get => ListBox.FillColor;
set => ListBox.FillColor = value;
}
[DefaultValue(typeof(Color), "48, 48, 48")]
public Color ItemForeColor
{
get => ListBox.ForeColor;
set => ListBox.ForeColor = value;
}
[DefaultValue(typeof(Color), "243, 249, 255")]
public Color ItemSelectForeColor
{
get => ListBox.ItemSelectForeColor;
set => ListBox.ItemSelectForeColor = value;
}
[DefaultValue(typeof(Color), "80, 160, 255")]
public Color ItemSelectBackColor
{
get => ListBox.ItemSelectBackColor;
set => ListBox.ItemSelectBackColor = value;
}
[DefaultValue(typeof(Color), "220, 236, 255")]
public Color ItemHoverColor
{
get => ListBox.HoverColor;
set => ListBox.HoverColor = value;
}
2020-05-11 21:11:29 +08:00
}
}