* UIComboBox: 增加清除按钮

This commit is contained in:
Sunny 2023-02-04 21:09:12 +08:00
parent d4de09d068
commit 4767ad6e01
2 changed files with 69 additions and 2 deletions

View File

@ -69,6 +69,8 @@ namespace Sunny.UI
fillColor = Color.White;
edit.BackColor = Color.White;
MouseMove += UIDropControl_MouseMove;
ControlBoxRect = new Rectangle(Width - 24, 0, 24, Height);
}
protected override void OnContextMenuStripChanged(EventArgs e)
@ -340,11 +342,54 @@ namespace Sunny.UI
g.FillRectangle(GetFillColor(), new Rectangle(Width - 27, Radius / 2, 26, Height - Radius));
Color color = GetRectColor();
SizeF sf = g.GetFontImageSize(dropSymbol, 24);
g.DrawFontImage(dropSymbol, 24, color, Width - 28 + (12 - sf.Width / 2.0f), (Height - sf.Height) / 2.0f);
int symbol = dropSymbol;
if (NeedDrawClearButton)
{
symbol = 261527;
SizeF sf = g.GetFontImageSize(symbol, 24);
g.DrawFontImage(symbol, 24, color, Width - 28 + (12 - sf.Width / 2.0f), (Height - sf.Height) / 2.0f, 2, 2);
}
else
{
SizeF sf = g.GetFontImageSize(symbol, 24);
g.DrawFontImage(symbol, 24, color, Width - 28 + (12 - sf.Width / 2.0f), (Height - sf.Height) / 2.0f);
}
//g.DrawLine(RectColor, Width - 1, Radius / 2, Width - 1, Height - Radius);
}
protected bool NeedDrawClearButton;
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (!showClearButton)
{
NeedDrawClearButton = false;
return;
}
bool inControlBox = e.Location.InRect(ControlBoxRect);
if (inControlBox != NeedDrawClearButton && Text.IsValid())
{
NeedDrawClearButton = inControlBox;
Invalidate();
}
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
if (NeedDrawClearButton)
{
NeedDrawClearButton = false;
Invalidate();
}
}
Rectangle ControlBoxRect;
public bool showClearButton;
protected override void OnGotFocus(EventArgs e)
{
base.OnGotFocus(e);

View File

@ -33,6 +33,7 @@
* 2022-11-03: V3.2.6
* 2022-11-13: V3.2.8
* 2022-11-30: V3.3.0 Clear方法
* 2023-02-04: V3.3.1
******************************************************************************/
using System;
@ -75,6 +76,14 @@ namespace Sunny.UI
CreateInstance();
}
[DefaultValue(false)]
[Description("显示清除按钮"), Category("SunnyUI")]
public bool ShowClearButton
{
get => showClearButton;
set => showClearButton = value;
}
public override void Clear()
{
base.Clear();
@ -591,6 +600,19 @@ namespace Sunny.UI
private void UIComboBox_ButtonClick(object sender, EventArgs e)
{
if (NeedDrawClearButton)
{
NeedDrawClearButton = false;
Text = "";
if (!showFilter)
dropForm.ListBox.SelectedIndex = -1;
else
filterForm.ListBox.SelectedIndex = -1;
Invalidate();
return;
}
if (!ShowFilter)
{
if (Items.Count > 0)