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 QQ:17612584 EMail:SunnyUI@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-12 20:17:55 +08:00
|
|
|
|
* 2020-06-11: V2.2.5 增加DataSource,支持数据绑定
|
2021-08-17 16:55:01 +08:00
|
|
|
|
* 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 23:11:35 +08:00
|
|
|
|
* 2022-01-16: V3.1.0 增加了下拉框颜色设置
|
2022-04-13 19:06:28 +08:00
|
|
|
|
* 2022-04-13: V3.1.3 根据Text自动选中SelectIndex
|
2022-04-15 23:41:32 +08:00
|
|
|
|
* 2022-04-15: V3.1.3 增加过滤
|
2022-04-16 22:03:20 +08:00
|
|
|
|
* 2022-04-16: V3.1.3 过滤下拉控跟随主题配色
|
2020-05-11 21:11:29 +08:00
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
|
|
using System;
|
2022-04-13 19:06:28 +08:00
|
|
|
|
using System.Collections.Generic;
|
2020-05-11 21:11:29 +08:00
|
|
|
|
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();
|
2021-06-03 12:22:31 +08:00
|
|
|
|
ListBox.SelectedIndexChanged += Box_SelectedIndexChanged;
|
|
|
|
|
ListBox.ValueMemberChanged += Box_ValueMemberChanged;
|
2021-06-03 13:28:00 +08:00
|
|
|
|
ListBox.SelectedValueChanged += ListBox_SelectedValueChanged;
|
2021-08-04 11:09:13 +08:00
|
|
|
|
ListBox.ItemsClear += ListBox_ItemsClear;
|
|
|
|
|
ListBox.ItemsRemove += ListBox_ItemsRemove;
|
2022-04-15 23:41:32 +08:00
|
|
|
|
|
2022-04-16 20:15:40 +08:00
|
|
|
|
filterForm.BeforeListClick += ListBox_Click;
|
|
|
|
|
|
2021-06-15 11:53:27 +08:00
|
|
|
|
edit.TextChanged += Edit_TextChanged;
|
2022-04-15 23:41:32 +08:00
|
|
|
|
edit.KeyDown += Edit_KeyDown;
|
2020-12-28 23:18:33 +08:00
|
|
|
|
DropDownWidth = 150;
|
2021-03-13 09:53:28 +08:00
|
|
|
|
fullControlSelect = true;
|
2020-06-11 23:53:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-16 20:15:40 +08:00
|
|
|
|
private void ListBox_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
SelectTextChange = true;
|
|
|
|
|
filterSelectedItem = filterList[(int)sender];
|
|
|
|
|
filterSelectedValue = GetItemText(filterSelectedItem);
|
|
|
|
|
Text = filterSelectedValue.ToString();
|
|
|
|
|
edit.SelectionStart = Text.Length;
|
|
|
|
|
SelectedValueChanged?.Invoke(this, EventArgs.Empty);
|
|
|
|
|
SelectTextChange = false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-15 23:41:32 +08:00
|
|
|
|
private void ShowDropDownFilter()
|
2022-04-13 19:06:28 +08:00
|
|
|
|
{
|
2022-04-15 23:41:32 +08:00
|
|
|
|
FilterItemForm.AutoClose = false;
|
|
|
|
|
if (!FilterItemForm.Visible)
|
|
|
|
|
{
|
|
|
|
|
FilterItemForm.Show(this, new Size(DropDownWidth < Width ? Width : DropDownWidth, CalcItemFormHeight()));
|
|
|
|
|
edit.Focus();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Edit_KeyDown(object sender, KeyEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (ShowFilter)
|
|
|
|
|
{
|
|
|
|
|
int cnt = filterForm.ListBox.Items.Count;
|
|
|
|
|
int idx = filterForm.ListBox.SelectedIndex;
|
|
|
|
|
|
|
|
|
|
if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Up)
|
|
|
|
|
{
|
|
|
|
|
ShowDropDownFilter();
|
|
|
|
|
if (cnt > 0)
|
|
|
|
|
{
|
|
|
|
|
if (e.KeyCode == Keys.Down)
|
|
|
|
|
{
|
|
|
|
|
if (idx < cnt - 1)
|
|
|
|
|
filterForm.ListBox.SelectedIndex++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (e.KeyCode == Keys.Up)
|
|
|
|
|
{
|
|
|
|
|
if (idx > 0)
|
|
|
|
|
filterForm.ListBox.SelectedIndex--;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (e.KeyCode == Keys.Escape)
|
|
|
|
|
{
|
|
|
|
|
FilterItemForm.Close();
|
|
|
|
|
}
|
|
|
|
|
else if (e.KeyCode == Keys.Return)
|
|
|
|
|
{
|
|
|
|
|
if (FilterItemForm.Visible)
|
|
|
|
|
{
|
|
|
|
|
if (cnt > 0 && idx >= 0 && idx < cnt)
|
|
|
|
|
{
|
|
|
|
|
SelectTextChange = true;
|
2022-04-16 20:15:40 +08:00
|
|
|
|
filterSelectedItem = filterList[idx];
|
|
|
|
|
filterSelectedValue = GetItemText(filterSelectedItem);
|
|
|
|
|
Text = filterSelectedValue.ToString();
|
2022-04-15 23:41:32 +08:00
|
|
|
|
edit.SelectionStart = Text.Length;
|
|
|
|
|
SelectedValueChanged?.Invoke(this, EventArgs.Empty);
|
|
|
|
|
SelectTextChange = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FilterItemForm.Close();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ShowDropDownFilter();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
base.OnKeyDown(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
2022-04-13 19:06:28 +08:00
|
|
|
|
{
|
2022-04-15 23:41:32 +08:00
|
|
|
|
if (e.KeyCode == Keys.Enter)
|
2022-04-13 19:06:28 +08:00
|
|
|
|
{
|
2022-04-15 23:41:32 +08:00
|
|
|
|
ShowDropDown();
|
|
|
|
|
}
|
|
|
|
|
else if (e.KeyCode == Keys.Escape)
|
|
|
|
|
{
|
|
|
|
|
ItemForm.Close();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
base.OnKeyDown(e);
|
2022-04-13 19:06:28 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-16 20:15:40 +08:00
|
|
|
|
private object filterSelectedItem;
|
|
|
|
|
private object filterSelectedValue;
|
2022-04-15 23:41:32 +08:00
|
|
|
|
private bool showFilter;
|
|
|
|
|
|
|
|
|
|
[DefaultValue(false)]
|
|
|
|
|
[Description("显示过滤"), Category("SunnyUI")]
|
|
|
|
|
public bool ShowFilter
|
|
|
|
|
{
|
|
|
|
|
get => showFilter;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
showFilter = value;
|
|
|
|
|
if (value)
|
|
|
|
|
{
|
|
|
|
|
DropDownStyle = UIDropDownStyle.DropDown;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[DefaultValue(false)]
|
|
|
|
|
[Description("过滤显示最大条目数"), Category("SunnyUI")]
|
|
|
|
|
public int FilterMaxCount { get; set; } = 50;
|
|
|
|
|
|
|
|
|
|
protected override void DropDownStyleChanged()
|
|
|
|
|
{
|
|
|
|
|
if (DropDownStyle == UIDropDownStyle.DropDownList)
|
|
|
|
|
{
|
|
|
|
|
showFilter = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CurrencyManager dataManager;
|
|
|
|
|
|
2022-04-16 20:15:40 +08:00
|
|
|
|
private void SetDataConnection()
|
2022-04-15 23:41:32 +08:00
|
|
|
|
{
|
|
|
|
|
if (DropDownStyle == UIDropDownStyle.DropDown && DataSource != null && DisplayMember.IsValid())
|
|
|
|
|
{
|
|
|
|
|
dataManager = (CurrencyManager)BindingContext[DataSource, new BindingMemberInfo(DisplayMember).BindingPath];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-16 21:51:03 +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);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-04 11:09:13 +08:00
|
|
|
|
private void ListBox_ItemsRemove(object sender, EventArgs e)
|
2021-08-03 14:14:15 +08:00
|
|
|
|
{
|
|
|
|
|
if (ListBox.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
Text = "";
|
|
|
|
|
edit.Text = "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-04 11:09:13 +08:00
|
|
|
|
private void ListBox_ItemsClear(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Text = "";
|
|
|
|
|
edit.Text = "";
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-06 09:52:33 +08:00
|
|
|
|
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);
|
2022-04-15 23:41:32 +08:00
|
|
|
|
if (DropDownStyle == UIDropDownStyle.DropDownList) return;
|
|
|
|
|
|
|
|
|
|
if (!ShowFilter)
|
2022-04-13 19:06:28 +08:00
|
|
|
|
{
|
2022-04-15 23:41:32 +08:00
|
|
|
|
if (SelectTextChange) return;
|
|
|
|
|
if (Text.IsValid())
|
2022-04-13 19:06:28 +08:00
|
|
|
|
{
|
2022-04-15 23:41:32 +08:00
|
|
|
|
ListBox.ListBox.Text = Text;
|
2022-04-13 19:06:28 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-04-15 23:41:32 +08:00
|
|
|
|
SelectTextChange = true;
|
|
|
|
|
SelectedIndex = -1;
|
|
|
|
|
edit.Text = "";
|
|
|
|
|
SelectTextChange = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (edit.Focused && Text.IsValid())
|
|
|
|
|
{
|
|
|
|
|
ShowDropDownFilter();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
filterForm.ListBox.Items.Clear();
|
|
|
|
|
if (Text.IsValid())
|
|
|
|
|
{
|
2022-04-16 20:15:40 +08:00
|
|
|
|
filterList.Clear();
|
|
|
|
|
|
2022-04-15 23:41:32 +08:00
|
|
|
|
if (DataSource == null)
|
|
|
|
|
{
|
|
|
|
|
foreach (var item in Items)
|
|
|
|
|
{
|
|
|
|
|
if (item.ToString().Contains(Text))
|
|
|
|
|
{
|
2022-04-16 20:15:40 +08:00
|
|
|
|
filterList.Add(item.ToString());
|
|
|
|
|
if (filterList.Count > FilterMaxCount) break;
|
2022-04-15 23:41:32 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (dataManager != null)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < Items.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
if (GetItemText(dataManager.List[i]).ToString().Contains(Text))
|
|
|
|
|
{
|
2022-04-16 20:15:40 +08:00
|
|
|
|
filterList.Add(dataManager.List[i]);
|
|
|
|
|
if (filterList.Count > FilterMaxCount) break;
|
2022-04-15 23:41:32 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-16 20:15:40 +08:00
|
|
|
|
|
|
|
|
|
foreach (var item in filterList)
|
|
|
|
|
{
|
|
|
|
|
filterForm.ListBox.Items.Add(GetItemText(item));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
filterSelectedItem = null;
|
|
|
|
|
filterSelectedValue = null;
|
2022-04-13 19:06:28 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-15 11:53:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-16 20:15:40 +08:00
|
|
|
|
List<object> filterList = new List<object>();
|
|
|
|
|
|
2021-06-03 13:28:00 +08:00
|
|
|
|
private void ListBox_SelectedValueChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
2022-04-16 20:15:40 +08:00
|
|
|
|
if (!ShowFilter)
|
|
|
|
|
SelectedValueChanged?.Invoke(this, e);
|
2021-06-03 13:28:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-11 23:53:51 +08:00
|
|
|
|
private void Box_ValueMemberChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
2021-07-05 22:09:10 +08:00
|
|
|
|
ValueMemberChanged?.Invoke(this, e);
|
2020-06-11 23:53:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Box_DisplayMemberChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
2021-07-05 22:09:10 +08:00
|
|
|
|
DisplayMemberChanged?.Invoke(this, e);
|
2022-04-16 20:15:40 +08:00
|
|
|
|
SetDataConnection();
|
2020-06-11 23:53:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Box_DataSourceChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
2021-07-05 22:09:10 +08:00
|
|
|
|
DataSourceChanged?.Invoke(this, e);
|
2022-04-16 20:15:40 +08:00
|
|
|
|
SetDataConnection();
|
2020-06-11 23:53:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-13 19:06:28 +08:00
|
|
|
|
private bool SelectTextChange;
|
|
|
|
|
|
2020-06-11 23:53:51 +08:00
|
|
|
|
private void Box_SelectedIndexChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
2022-04-13 19:06:28 +08:00
|
|
|
|
SelectTextChange = true;
|
2022-04-15 23:41:32 +08:00
|
|
|
|
if (ListBox.SelectedItem != null && !ShowFilter)
|
2022-04-13 19:06:28 +08:00
|
|
|
|
Text = ListBox.GetItemText(ListBox.SelectedItem);
|
|
|
|
|
SelectTextChange = false;
|
2020-07-29 20:14:59 +08:00
|
|
|
|
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
|
|
|
|
Invalidate();
|
2020-05-11 21:11:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-12 20:17:55 +08:00
|
|
|
|
private readonly UIComboBoxItem dropForm = new UIComboBoxItem();
|
2022-04-15 23:41:32 +08:00
|
|
|
|
private readonly UIComboBoxItem filterForm = new UIComboBoxItem();
|
|
|
|
|
|
|
|
|
|
private UIDropDown filterItemForm;
|
|
|
|
|
|
|
|
|
|
private UIDropDown FilterItemForm
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (filterItemForm == null)
|
|
|
|
|
{
|
|
|
|
|
filterItemForm = new UIDropDown(filterForm);
|
|
|
|
|
|
|
|
|
|
if (filterItemForm != null)
|
|
|
|
|
{
|
|
|
|
|
filterItemForm.VisibleChanged += FilterItemForm_VisibleChanged;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return filterItemForm;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void FilterItemForm_VisibleChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
dropSymbol = SymbolNormal;
|
|
|
|
|
if (filterItemForm.Visible)
|
|
|
|
|
{
|
|
|
|
|
dropSymbol = SymbolDropDown;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Invalidate();
|
|
|
|
|
}
|
2020-05-11 21:11:29 +08:00
|
|
|
|
|
|
|
|
|
protected override void CreateInstance()
|
|
|
|
|
{
|
2020-06-12 20:17:55 +08:00
|
|
|
|
ItemForm = new UIDropDown(dropForm);
|
2020-05-11 21:11:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override int CalcItemFormHeight()
|
|
|
|
|
{
|
|
|
|
|
int interval = ItemForm.Height - ItemForm.ClientRectangle.Height;
|
2020-06-12 20:17:55 +08:00
|
|
|
|
return 4 + Math.Min(ListBox.Items.Count, MaxDropDownItems) * ItemHeight + interval;
|
2020-05-11 21:11:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private UIListBox ListBox
|
|
|
|
|
{
|
2020-06-12 20:17:55 +08:00
|
|
|
|
get => dropForm.ListBox;
|
2020-05-11 21:11:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-16 20:15:40 +08:00
|
|
|
|
private UIListBox FilterListBox
|
2022-04-15 23:41:32 +08:00
|
|
|
|
{
|
2022-04-16 22:03:20 +08:00
|
|
|
|
get => filterForm.ListBox;
|
2022-04-15 23:41:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-11 21:11:29 +08:00
|
|
|
|
[DefaultValue(25)]
|
2020-08-08 21:55:50 +08:00
|
|
|
|
[Description("列表项高度"), Category("SunnyUI")]
|
2020-05-11 21:11:29 +08:00
|
|
|
|
public int ItemHeight
|
|
|
|
|
{
|
|
|
|
|
get => ListBox.ItemHeight;
|
2022-04-16 20:15:40 +08:00
|
|
|
|
set => FilterListBox.ItemHeight = ListBox.ItemHeight = value;
|
2020-05-11 21:11:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[DefaultValue(8)]
|
2020-08-08 21:55:50 +08:00
|
|
|
|
[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
|
|
|
|
}
|
2022-04-15 23:41:32 +08:00
|
|
|
|
|
|
|
|
|
if (filterForm != null)
|
|
|
|
|
{
|
|
|
|
|
filterForm.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)
|
|
|
|
|
{
|
2022-04-15 23:41:32 +08:00
|
|
|
|
if (!ShowFilter)
|
2022-04-13 19:06:28 +08:00
|
|
|
|
{
|
2022-04-15 23:41:32 +08:00
|
|
|
|
if (Items.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
ItemForm.Show(this, new Size(DropDownWidth < Width ? Width : DropDownWidth, CalcItemFormHeight()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (FilterItemForm.Visible)
|
|
|
|
|
{
|
|
|
|
|
FilterItemForm.Close();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ShowDropDownFilter();
|
|
|
|
|
}
|
2022-04-13 19:06:28 +08:00
|
|
|
|
}
|
2020-05-11 21:11:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void SetStyleColor(UIBaseStyle uiColor)
|
|
|
|
|
{
|
|
|
|
|
base.SetStyleColor(uiColor);
|
2022-03-29 23:55:38 +08:00
|
|
|
|
ListBox.SetStyleColor(uiColor.DropDownStyle);
|
2022-04-16 21:19:31 +08:00
|
|
|
|
FilterListBox.SetStyleColor(uiColor.DropDownStyle);
|
2020-05-11 21:11:29 +08:00
|
|
|
|
}
|
2020-06-11 23:53:51 +08:00
|
|
|
|
|
|
|
|
|
public object DataSource
|
|
|
|
|
{
|
2021-06-03 12:22:31 +08:00
|
|
|
|
get => ListBox.DataSource;
|
2022-04-13 19:06:28 +08:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
ListBox.DataSource = value;
|
|
|
|
|
Box_DataSourceChanged(this, EventArgs.Empty);
|
|
|
|
|
}
|
2020-06-11 23:53:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 23:18:33 +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)]
|
2021-06-03 12:22:31 +08:00
|
|
|
|
[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)]
|
2021-06-03 12:22:31 +08:00
|
|
|
|
[Description("列表项"), Category("SunnyUI")]
|
|
|
|
|
public ListBox.ObjectCollection Items => ListBox.Items;
|
2020-06-11 23:53:51 +08:00
|
|
|
|
|
|
|
|
|
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
|
2020-08-08 21:55:50 +08:00
|
|
|
|
[Description("选中索引"), Category("SunnyUI")]
|
2020-06-11 23:53:51 +08:00
|
|
|
|
public int SelectedIndex
|
|
|
|
|
{
|
2022-04-16 20:15:40 +08:00
|
|
|
|
get => ShowFilter ? -1 : ListBox.SelectedIndex;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (!ShowFilter)
|
|
|
|
|
{
|
|
|
|
|
ListBox.SelectedIndex = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-11 23:53:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Browsable(false), Bindable(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
|
2020-08-08 21:55:50 +08:00
|
|
|
|
[Description("选中项"), Category("SunnyUI")]
|
2020-06-11 23:53:51 +08:00
|
|
|
|
public object SelectedItem
|
|
|
|
|
{
|
2022-04-16 20:15:40 +08:00
|
|
|
|
get => ShowFilter ? filterSelectedItem : ListBox.SelectedItem;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (!ShowFilter)
|
|
|
|
|
{
|
|
|
|
|
ListBox.SelectedItem = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-11 23:53:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
|
2020-08-08 21:55:50 +08:00
|
|
|
|
[Description("选中文字"), Category("SunnyUI")]
|
2020-06-11 23:53:51 +08:00
|
|
|
|
public string SelectedText
|
|
|
|
|
{
|
2020-06-12 20:17:55 +08:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (DropDownStyle == UIDropDownStyle.DropDown)
|
|
|
|
|
{
|
|
|
|
|
return edit.SelectedText;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return Text;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-11 23:53:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void ResetText()
|
|
|
|
|
{
|
2020-06-12 20:17:55 +08:00
|
|
|
|
Clear();
|
2020-06-11 23:53:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-08 21:55:50 +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
|
|
|
|
|
{
|
2021-06-03 12:22:31 +08:00
|
|
|
|
get => ListBox.DisplayMember;
|
2022-04-13 19:06:28 +08:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
ListBox.DisplayMember = value;
|
|
|
|
|
Box_DisplayMemberChanged(this, EventArgs.Empty);
|
|
|
|
|
}
|
2020-06-11 23:53:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-08 21:55:50 +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
|
|
|
|
|
{
|
2021-06-03 12:22:31 +08:00
|
|
|
|
get => ListBox.FormatString;
|
2022-04-16 20:15:40 +08:00
|
|
|
|
set => FilterListBox.FormatString = ListBox.FormatString = value;
|
2020-06-11 23:53:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-08 21:55:50 +08:00
|
|
|
|
[Description("获取或设置指示显示值是否可以进行格式化操作。"), Category("SunnyUI")]
|
2020-06-11 23:53:51 +08:00
|
|
|
|
[DefaultValue(false)]
|
|
|
|
|
public bool FormattingEnabled
|
|
|
|
|
{
|
2021-06-03 12:22:31 +08:00
|
|
|
|
get => ListBox.FormattingEnabled;
|
2022-04-16 20:15:40 +08:00
|
|
|
|
set => FilterListBox.FormattingEnabled = ListBox.FormattingEnabled = value;
|
2020-06-11 23:53:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-08 21:55:50 +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
|
|
|
|
|
{
|
2021-06-03 12:22:31 +08:00
|
|
|
|
get => ListBox.ValueMember;
|
|
|
|
|
set => ListBox.ValueMember = value;
|
2020-06-12 20:17:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-11 23:53:51 +08:00
|
|
|
|
[
|
2020-06-12 20:17:55 +08:00
|
|
|
|
DefaultValue(null),
|
|
|
|
|
Browsable(false),
|
|
|
|
|
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
|
|
|
|
|
Bindable(true)
|
|
|
|
|
]
|
2020-06-11 23:53:51 +08:00
|
|
|
|
public object SelectedValue
|
|
|
|
|
{
|
2022-04-16 20:15:40 +08:00
|
|
|
|
get => ShowFilter ? filterSelectedValue : ListBox.SelectedValue;
|
2022-04-15 23:41:32 +08:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (!ShowFilter)
|
|
|
|
|
ListBox.SelectedValue = value;
|
|
|
|
|
}
|
2020-06-11 23:53:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetItemText(object item)
|
|
|
|
|
{
|
2022-04-16 22:03:20 +08:00
|
|
|
|
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)
|
|
|
|
|
{
|
2022-04-15 23:41:32 +08:00
|
|
|
|
if (e.KeyCode == Keys.Enter && !ShowFilter)
|
2021-08-23 10:51:09 +08:00
|
|
|
|
{
|
|
|
|
|
ShowDropDown();
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-16 23:11:35 +08:00
|
|
|
|
|
|
|
|
|
[DefaultValue(typeof(Color), "White")]
|
|
|
|
|
public Color ItemFillColor
|
|
|
|
|
{
|
|
|
|
|
get => ListBox.FillColor;
|
2022-04-16 20:15:40 +08:00
|
|
|
|
set => FilterListBox.FillColor = ListBox.FillColor = value;
|
2022-01-16 23:11:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[DefaultValue(typeof(Color), "48, 48, 48")]
|
|
|
|
|
public Color ItemForeColor
|
|
|
|
|
{
|
|
|
|
|
get => ListBox.ForeColor;
|
2022-04-16 20:15:40 +08:00
|
|
|
|
set => FilterListBox.ForeColor = ListBox.ForeColor = value;
|
2022-01-16 23:11:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-20 17:16:40 +08:00
|
|
|
|
[DefaultValue(typeof(Color), "243, 249, 255")]
|
2022-01-16 23:11:35 +08:00
|
|
|
|
public Color ItemSelectForeColor
|
|
|
|
|
{
|
|
|
|
|
get => ListBox.ItemSelectForeColor;
|
2022-04-16 20:15:40 +08:00
|
|
|
|
set => FilterListBox.ItemSelectForeColor = ListBox.ItemSelectForeColor = value;
|
2022-01-16 23:11:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[DefaultValue(typeof(Color), "80, 160, 255")]
|
|
|
|
|
public Color ItemSelectBackColor
|
|
|
|
|
{
|
|
|
|
|
get => ListBox.ItemSelectBackColor;
|
2022-04-16 20:15:40 +08:00
|
|
|
|
set => FilterListBox.ItemSelectBackColor = ListBox.ItemSelectBackColor = value;
|
2022-01-16 23:11:35 +08:00
|
|
|
|
}
|
2022-01-17 11:08:25 +08:00
|
|
|
|
|
2022-03-20 17:16:40 +08:00
|
|
|
|
[DefaultValue(typeof(Color), "220, 236, 255")]
|
2022-01-17 11:08:25 +08:00
|
|
|
|
public Color ItemHoverColor
|
|
|
|
|
{
|
|
|
|
|
get => ListBox.HoverColor;
|
2022-04-16 20:15:40 +08:00
|
|
|
|
set => FilterListBox.HoverColor = ListBox.HoverColor = value;
|
2022-01-17 11:08:25 +08:00
|
|
|
|
}
|
2020-05-11 21:11:29 +08:00
|
|
|
|
}
|
|
|
|
|
}
|