diff --git a/Bin/SunnyUI.dll b/Bin/SunnyUI.dll index 6d97d21b..faaf803b 100644 Binary files a/Bin/SunnyUI.dll and b/Bin/SunnyUI.dll differ diff --git a/Bin/SunnyUI.pdb b/Bin/SunnyUI.pdb index a7dd4f87..2abecb18 100644 Binary files a/Bin/SunnyUI.pdb and b/Bin/SunnyUI.pdb differ diff --git a/SunnyUI.Demo/Bin/SunnyUI.Demo.exe b/SunnyUI.Demo/Bin/SunnyUI.Demo.exe index 9704bbb1..a3ed4f2f 100644 Binary files a/SunnyUI.Demo/Bin/SunnyUI.Demo.exe and b/SunnyUI.Demo/Bin/SunnyUI.Demo.exe differ diff --git a/SunnyUI.Demo/Bin/SunnyUI.dll b/SunnyUI.Demo/Bin/SunnyUI.dll index 6d97d21b..faaf803b 100644 Binary files a/SunnyUI.Demo/Bin/SunnyUI.dll and b/SunnyUI.Demo/Bin/SunnyUI.dll differ diff --git a/SunnyUI.Demo/Controls/FCombobox.Designer.cs b/SunnyUI.Demo/Controls/FCombobox.Designer.cs index 8560484d..8079d6b3 100644 --- a/SunnyUI.Demo/Controls/FCombobox.Designer.cs +++ b/SunnyUI.Demo/Controls/FCombobox.Designer.cs @@ -182,7 +182,6 @@ // uiComboBox2 // this.uiComboBox2.DropDownStyle = Sunny.UI.UIDropDownStyle.DropDownList; - this.uiComboBox2.DropDownWidth = 0; this.uiComboBox2.FillColor = System.Drawing.Color.White; this.uiComboBox2.Font = new System.Drawing.Font("微软雅黑", 12F); this.uiComboBox2.Items.AddRange(new object[] { @@ -203,7 +202,6 @@ // // uiComboBox1 // - this.uiComboBox1.DropDownWidth = 0; this.uiComboBox1.FillColor = System.Drawing.Color.White; this.uiComboBox1.Font = new System.Drawing.Font("微软雅黑", 12F); this.uiComboBox1.Items.AddRange(new object[] { diff --git a/SunnyUI/Controls/DropItem/UIDropControl.cs b/SunnyUI/Controls/DropItem/UIDropControl.cs index ab352b21..4a8eda9a 100644 --- a/SunnyUI/Controls/DropItem/UIDropControl.cs +++ b/SunnyUI/Controls/DropItem/UIDropControl.cs @@ -96,7 +96,7 @@ namespace Sunny.UI { dropSymbol = SymbolNormal; - if (itemForm != null && itemForm.Visible) + if (DroppedDown) { dropSymbol = SymbolDropDown; } @@ -105,7 +105,7 @@ namespace Sunny.UI } [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] - public bool DroppedDown=> itemForm != null && itemForm.Visible; + public bool DroppedDown => itemForm != null && itemForm.Visible; private int symbolNormal = 61703; private int dropSymbol = 61703; @@ -159,7 +159,7 @@ namespace Sunny.UI public event EventHandler ButtonClick; - private readonly TextBoxEx edit = new TextBoxEx(); + protected readonly TextBoxEx edit = new TextBoxEx(); protected override void OnTextChanged(EventArgs e) { @@ -270,14 +270,14 @@ namespace Sunny.UI set => edit.MaxLength = Math.Max(value, 1); } - [Browsable(false),DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] + [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public int SelectionLength { get => edit.SelectionLength; set => edit.SelectionLength = value; } - [ Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden) ] + [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public int SelectionStart { get => edit.SelectionStart; @@ -319,7 +319,7 @@ namespace Sunny.UI } ButtonClick?.Invoke(this, e); - } + } } //public event EventHandler DropDown; @@ -336,7 +336,7 @@ namespace Sunny.UI edit.SelectAll(); } - private class TextBoxEx : TextBox + protected class TextBoxEx : TextBox { private string watermark; diff --git a/SunnyUI/Controls/UIComboBox.cs b/SunnyUI/Controls/UIComboBox.cs index 16ae399f..4f4a0920 100644 --- a/SunnyUI/Controls/UIComboBox.cs +++ b/SunnyUI/Controls/UIComboBox.cs @@ -17,7 +17,7 @@ * 创建日期: 2020-01-01 * * 2020-01-01: V2.2.0 增加文件说明 - * 2020-06-11: V2.2.5 增加DataSource + * 2020-06-11: V2.2.5 增加DataSource,支持数据绑定 ******************************************************************************/ using System; @@ -40,21 +40,9 @@ namespace Sunny.UI InitializeComponent(); box.SelectedIndexChanged += Box_SelectedIndexChanged; - box.SelectionChangeCommitted += Box_SelectionChangeCommitted; - box.TextUpdate += Box_TextUpdate; box.DataSourceChanged += Box_DataSourceChanged; box.DisplayMemberChanged += Box_DisplayMemberChanged; - box.Format += Box_Format; - box.FormatInfoChanged += Box_FormatInfoChanged; - box.FormatStringChanged += Box_FormatStringChanged; - box.FormattingEnabledChanged += Box_FormattingEnabledChanged; box.ValueMemberChanged += Box_ValueMemberChanged; - box.SelectedValueChanged += Box_SelectedValueChanged; - } - - private void Box_SelectedValueChanged(object sender, EventArgs e) - { - SelectedValueChanged?.Invoke(sender, e); } private void Box_ValueMemberChanged(object sender, EventArgs e) @@ -62,26 +50,6 @@ namespace Sunny.UI ValueMemberChanged?.Invoke(sender, e); } - private void Box_FormattingEnabledChanged(object sender, EventArgs e) - { - FormattingEnabledChanged?.Invoke(sender, e); - } - - private void Box_FormatStringChanged(object sender, EventArgs e) - { - FormatStringChanged?.Invoke(sender, e); - } - - private void Box_FormatInfoChanged(object sender, EventArgs e) - { - FormatInfoChanged?.Invoke(sender, e); - } - - private void Box_Format(object sender, ListControlConvertEventArgs e) - { - Format?.Invoke(sender, e); - } - private void Box_DisplayMemberChanged(object sender, EventArgs e) { DisplayMemberChanged?.Invoke(sender, e); @@ -92,72 +60,48 @@ namespace Sunny.UI DataSourceChanged?.Invoke(sender, e); } - private void Box_TextUpdate(object sender, EventArgs e) - { - TextUpdate?.Invoke(sender, e); - } - - private void Box_SelectionChangeCommitted(object sender, EventArgs e) - { - SelectionChangeCommitted?.Invoke(sender, e); - } - private void Box_SelectedIndexChanged(object sender, EventArgs e) { + Text = box.GetItemText(box.SelectedItem); + GetSelectedValue(); + SelectedValueChanged?.Invoke(this, e); SelectedIndexChanged?.Invoke(sender, e); } - public event EventHandler TextUpdate; - public event EventHandler SelectedIndexChanged; - public event EventHandler SelectionChangeCommitted; - public event EventHandler DataSourceChanged; public event EventHandler DisplayMemberChanged; - public event ListControlConvertEventHandler Format; - - public event EventHandler FormatStringChanged; - - public event EventHandler FormattingEnabledChanged; - public event EventHandler ValueMemberChanged; public event EventHandler SelectedValueChanged; - [Browsable(false), EditorBrowsable(EditorBrowsableState.Advanced)] - public event EventHandler FormatInfoChanged; - - private readonly ComboBox box = new ComboBox(); + public readonly ComboBox box = new ComboBox(); protected override void ItemForm_ValueChanged(object sender, object value) { - //selectedItem = ListBox.SelectedItem; - //selectedIndex = ListBox.SelectedIndex; - box.SelectedIndex = ListBox.SelectedIndex; - Text = ListBox.Text; + SelectedIndex = ListBox.SelectedIndex; Invalidate(); - //SelectedIndexChanged?.Invoke(this, null); } - private readonly UIComboBoxItem item = new UIComboBoxItem(); + private readonly UIComboBoxItem dropForm = new UIComboBoxItem(); protected override void CreateInstance() { - ItemForm = new UIDropDown(item); + ItemForm = new UIDropDown(dropForm); } protected override int CalcItemFormHeight() { int interval = ItemForm.Height - ItemForm.ClientRectangle.Height; - return 4 + Math.Min(Items.Count, MaxDropDownItems) * ItemHeight + interval; + return 4 + Math.Min(ListBox.Items.Count, MaxDropDownItems) * ItemHeight + interval; } private UIListBox ListBox { - get => item.ListBox; + get => dropForm.ListBox; } [DefaultValue(25)] @@ -170,12 +114,6 @@ namespace Sunny.UI [DefaultValue(8)] public int MaxDropDownItems { get; set; } = 8; - //[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] - //[Localizable(true)] - //[Editor("System.Windows.Forms.Design.ListControlStringCollectionEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))] - //[MergableProperty(false)] - //public ListBox.ObjectCollection Items => ListBox?.Items; - private void UIComboBox_FontChanged(object sender, EventArgs e) { if (ItemForm != null) @@ -186,16 +124,15 @@ namespace Sunny.UI private void UIComboBox_ButtonClick(object sender, EventArgs e) { + ListBox.Items.Clear(); if (Items.Count == 0 || ItemForm.Visible) { return; } - ListBox.Items.Clear(); - - foreach (var item in Items) + foreach (var data in Items) { - ListBox.Items.Add(GetItemText(item)); + ListBox.Items.Add(GetItemText(data)); } ListBox.SelectedIndex = SelectedIndex; @@ -210,52 +147,58 @@ namespace Sunny.UI ListBox.SetStyleColor(uiColor); } - [ - DefaultValue(AutoCompleteMode.None), - Browsable(true), EditorBrowsable(EditorBrowsableState.Always) - ] - public AutoCompleteMode AutoCompleteMode - { - get => box.AutoCompleteMode; - set => box.AutoCompleteMode = value; - } - - [ - DefaultValue(AutoCompleteSource.None), - Browsable(true), EditorBrowsable(EditorBrowsableState.Always) - ] - public AutoCompleteSource AutoCompleteSource - { - get => box.AutoCompleteSource; - set => box.AutoCompleteSource = value; - } - - [Browsable(true)] - [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] - [Editor("System.Windows.Forms.Design.ListControlStringCollectionEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))] - [EditorBrowsable(EditorBrowsableState.Always)] - [Localizable(true)] - public AutoCompleteStringCollection AutoCompleteCustomSource - { - get => box.AutoCompleteCustomSource; - set => box.AutoCompleteCustomSource = value; - } + private object dataSource; [DefaultValue(null), RefreshProperties(RefreshProperties.Repaint), AttributeProvider(typeof(IListSource))] public object DataSource { - get => box.DataSource; - set => box.DataSource = value; + get => dataSource; + set + { + SetDataConnection(value, new BindingMemberInfo(DisplayMember)); + dataSource = value; + box.Items.Clear(); + + foreach (var obj in dataManager.List) + { + box.Items.Add(obj); + } + } } - [DefaultValue(DrawMode.Normal), RefreshProperties(RefreshProperties.Repaint)] - public DrawMode DrawMode + private bool inSetDataConnection; + private CurrencyManager dataManager; + + private void SetDataConnection(object newDataSource, BindingMemberInfo newDisplayMember) { - get => box.DrawMode; - set => box.DrawMode = value; + bool dataSourceChanged = dataSource != newDataSource; + bool displayMemberChanged = !DisplayMember.Equals(newDisplayMember.BindingPath); + + if (inSetDataConnection) + { + return; + } + + try + { + if (dataSourceChanged || displayMemberChanged) + { + CurrencyManager newDataManager = null; + if (newDataSource != null && newDataSource != Convert.DBNull) + { + newDataManager = (CurrencyManager)BindingContext[newDataSource, newDisplayMember.BindingPath]; + } + + dataManager = newDataManager; + } + } + finally + { + inSetDataConnection = false; + } } - public int DropDownWidth { get; set; } + //public int DropDownWidth { get; set; } [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] [Editor("System.Windows.Forms.Design.ListControlStringCollectionEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))] @@ -283,50 +226,22 @@ namespace Sunny.UI [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public string SelectedText { - get => box.SelectedText; - set => box.SelectedText = value; - } - - [DefaultValue(false)] - public bool Sorted - { - get => box.Sorted; - set => box.Sorted = value; - } - - public void BeginUpdate() - { - box.BeginUpdate(); - } - - public void EndUpdate() - { - box.EndUpdate(); - } - - public int FindString(string s) - { - return box.FindString(s, -1); - } - - public int FindString(string s, int startIndex) - { - return box.FindString(s, startIndex); - } - - public int FindStringExact(string s) - { - return box.FindStringExact(s); - } - - public int FindStringExact(string s, int startIndex) - { - return box.FindStringExact(s, startIndex); + get + { + if (DropDownStyle == UIDropDownStyle.DropDown) + { + return edit.SelectedText; + } + else + { + return Text; + } + } } public override void ResetText() { - box.ResetText(); + Clear(); } [DefaultValue("")] @@ -335,24 +250,17 @@ namespace Sunny.UI public string DisplayMember { get => box.DisplayMember; - set => box.DisplayMember = value; + set + { + SetDataConnection(dataSource, new BindingMemberInfo(value)); + box.DisplayMember = value; + } } - [ - Browsable(false), - EditorBrowsable(EditorBrowsableState.Advanced), - DefaultValue(null) - ] - public IFormatProvider FormatInfo - { - get => box.FormatInfo; - set => box.FormatInfo = value; - } [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 => box.FormatString; @@ -374,61 +282,95 @@ namespace Sunny.UI set => box.ValueMember = value; } + private object selectedValue; + + private void GetSelectedValue() + { + if (SelectedIndex != -1 && dataManager != null) + { + selectedValue = FilterItemOnProperty(SelectedItem, ValueMember); + } + else + { + selectedValue = null; + } + } + [ -DefaultValue(null), -Browsable(false), -DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), -Bindable(true) -] + DefaultValue(null), + Browsable(false), + DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), + Bindable(true) + ] public object SelectedValue { - get => box.SelectedValue; - set => box.SelectedValue = value; + get + { + GetSelectedValue(); + return selectedValue; + } + set + { + selectedValue = value; + + if (value == null) + { + return; + } + + if (dataManager != null) + { + int index = DataManagerFind(value); + SelectedIndex = index; + } + } + } + + private int DataManagerFind(object value) + { + if (dataManager == null) return -1; + for (int i = 0; i < dataManager.List.Count; i++) + { + var item = dataManager.List[i]; + if (FilterItemOnProperty(item, ValueMember).Equals(value)) + { + return i; + } + } + + return -1; + } + + private object FilterItemOnProperty(object item, string field) + { + if (item != null && field.Length > 0) + { + try + { + // if we have a dataSource, then use that to display the string + PropertyDescriptor descriptor; + if (dataManager != null) + descriptor = dataManager.GetItemProperties().Find(field, true); + else + descriptor = TypeDescriptor.GetProperties(item).Find(field, true); + + if (descriptor != null) + { + item = descriptor.GetValue(item); + } + } + catch + { + return null; + } + } + + return item; } public string GetItemText(object item) { return box.GetItemText(item); } - - //private int selectedIndex = -1; - - //[Browsable(false)] - //[DefaultValue(-1)] - //public int SelectedIndex - //{ - // get => selectedIndex; - // set - // { - // if (value.InRange(-1, ListBox.Items.Count - 1)) - // { - // ListBox.SelectedIndex = value; - // selectedIndex = value; - - // if (value >= 0) - // { - // Text = ListBox.Items[value].ToString(); - // } - // } - // } - //} - - //private object selectedItem; - - //[Browsable(false)] - //[DefaultValue(null)] - //public object SelectedItem - //{ - // get => selectedItem; - // set - // { - // if (value != null) - // { - // int idx = ListBox.Items.IndexOf(value); - // SelectedIndex = idx; - // selectedItem = idx >= 0 ? value : null; - // } - // } - //} } } \ No newline at end of file diff --git a/Updates.txt b/Updates.txt index 95e7fd63..54800027 100644 --- a/Updates.txt +++ b/Updates.txt @@ -1,5 +1,8 @@ + ӣ - ɾ * ޸ +2020.06.12 ++ UIComboBoxݰ󶨣DataSourceDisplayMemberValueMember + 2020.06.11 + ӵNuget * UIFrame޸ҳUIContextMenuStripʱʾλ