diff --git a/Bin/net40/SunnyUI.Demo.exe b/Bin/net40/SunnyUI.Demo.exe index 70390304..384e4593 100644 Binary files a/Bin/net40/SunnyUI.Demo.exe and b/Bin/net40/SunnyUI.Demo.exe differ diff --git a/Bin/net40/SunnyUI.dll b/Bin/net40/SunnyUI.dll index e6e7921e..7cc3ee24 100644 Binary files a/Bin/net40/SunnyUI.dll and b/Bin/net40/SunnyUI.dll differ diff --git a/Bin/net5.0-windows/SunnyUI.dll b/Bin/net5.0-windows/SunnyUI.dll index 021f4cc1..71f8457f 100644 Binary files a/Bin/net5.0-windows/SunnyUI.dll and b/Bin/net5.0-windows/SunnyUI.dll differ diff --git a/Bin/net5.0-windows/ref/SunnyUI.dll b/Bin/net5.0-windows/ref/SunnyUI.dll index a69033b3..209a44ca 100644 Binary files a/Bin/net5.0-windows/ref/SunnyUI.dll and b/Bin/net5.0-windows/ref/SunnyUI.dll differ diff --git a/Bin/netcoreapp3.1/SunnyUI.dll b/Bin/netcoreapp3.1/SunnyUI.dll index 54add391..cb0ffc01 100644 Binary files a/Bin/netcoreapp3.1/SunnyUI.dll and b/Bin/netcoreapp3.1/SunnyUI.dll differ diff --git a/SunnyUI.Demo/Forms/FEditor.cs b/SunnyUI.Demo/Forms/FEditor.cs index 0128082c..896ac0ab 100644 --- a/SunnyUI.Demo/Forms/FEditor.cs +++ b/SunnyUI.Demo/Forms/FEditor.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; namespace Sunny.UI.Demo.Forms { @@ -43,12 +44,24 @@ namespace Sunny.UI.Demo.Forms private void uiSymbolButton1_Click(object sender, EventArgs e) { + List infoList = new List(); + FCombobox.Info info1 = new FCombobox.Info() { Id = "1", Name = "张三" }; + FCombobox.Info info2 = new FCombobox.Info() { Id = "2", Name = "李四" }; + FCombobox.Info info3 = new FCombobox.Info() { Id = "3", Name = "王五" }; + infoList.Add(info1); + infoList.Add(info2); + infoList.Add(info3); + + string[] sex = new[] { "男", "女" }; + UIEditOption option = new UIEditOption(); option.AutoLabelWidth = true; option.Text = "增加"; option.AddText("Name", "姓名", "", true); option.AddInteger("Age", "年龄", 20); option.AddDate("Birthday", "生日", DateTime.Now); + option.AddCombobox("Sex", "性别", sex, 1, true, true); + option.AddCombobox("Info", "关联", infoList, "Name", "Id", "2"); UIEditForm frm = new UIEditForm(option); frm.ShowDialog(); @@ -58,6 +71,8 @@ namespace Sunny.UI.Demo.Forms Console.WriteLine("姓名: " + frm["Name"]); Console.WriteLine("年龄: " + frm["Age"]); Console.WriteLine("生日: " + frm["Birthday"]); + Console.WriteLine("性别: " + sex[(int)frm["Sex"]]); + Console.WriteLine("关联: " + frm["Info"]); } frm.Dispose(); diff --git a/SunnyUI/Forms/UIEditForm.cs b/SunnyUI/Forms/UIEditForm.cs index 8c29a168..3cffe630 100644 --- a/SunnyUI/Forms/UIEditForm.cs +++ b/SunnyUI/Forms/UIEditForm.cs @@ -164,6 +164,40 @@ namespace Sunny.UI ctrls.Add(edit); } + if (info.EditType == EditType.Combobox) + { + UIComboBox edit = new UIComboBox(); + edit.DropDownStyle = UIDropDownStyle.DropDownList; + edit.Left = option.LabelWidth; + edit.Width = info.HalfWidth ? option.ValueWidth / 2 : option.ValueWidth; + edit.Top = top; + edit.Parent = this; + edit.Name = "Edit_" + info.DataPropertyName; + edit.Enabled = info.Enabled; + + if (info.DisplayMember.IsNullOrEmpty()) + { + object[] items = (object[])info.DataSource; + if (items != null) + { + edit.Items.AddRange(items); + int index = info.Value.ToString().ToInt(); + if (index < items.Length) + edit.SelectedIndex = index; + } + } + else + { + edit.DisplayMember = info.DisplayMember; + edit.ValueMember = info.ValueMember; + edit.DataSource = info.DataSource; + edit.SelectedValue = info.Value; + } + + + ctrls.Add(edit); + } + top += 29 + 10; } @@ -308,6 +342,13 @@ namespace Sunny.UI if (edit == null) continue; info.Value = edit.Value; } + + if (info.EditType == EditType.Combobox) + { + UIComboBox edit = this.GetControl("Edit_" + info.DataPropertyName); + if (edit == null) continue; + info.Value = edit.ValueMember.IsValid() ? edit.SelectedValue : edit.SelectedIndex; + } } } diff --git a/SunnyUI/Forms/UIEditFormHelper.cs b/SunnyUI/Forms/UIEditFormHelper.cs index 6cd4de37..006bee6c 100644 --- a/SunnyUI/Forms/UIEditFormHelper.cs +++ b/SunnyUI/Forms/UIEditFormHelper.cs @@ -20,6 +20,7 @@ ******************************************************************************/ using System; +using System.Collections; using System.Collections.Concurrent; using System.Collections.Generic; using System.Data; @@ -33,7 +34,8 @@ namespace Sunny.UI Double, Date, DateTime, - Password + Password, + Combobox } public class EditInfo @@ -51,6 +53,10 @@ namespace Sunny.UI public bool Enabled { get; set; } public bool HalfWidth { get; set; } + + public object DataSource { get; set; } + public string DisplayMember { get; set; } + public string ValueMember { get; set; } } public class UIEditOption @@ -180,5 +186,48 @@ namespace Sunny.UI Infos.Add(info); Dictionary.TryAdd(info.DataPropertyName, info); } + + public void AddCombobox(string dataPropertyName, string text, IList dataSource, string displayMember, + string valueMember, object value, bool enabled = true, bool halfWidth = false) + { + if (Dictionary.ContainsKey(dataPropertyName)) + throw new DuplicateNameException(dataPropertyName + ": 已经存在"); + + EditInfo info = new EditInfo() + { + DataPropertyName = dataPropertyName, + EditType = EditType.Combobox, + Text = text, + Value = value, + Enabled = enabled, + HalfWidth = halfWidth, + DataSource = dataSource, + DisplayMember = displayMember, + ValueMember = valueMember + }; + + Infos.Add(info); + Dictionary.TryAdd(info.DataPropertyName, info); + } + + public void AddCombobox(string dataPropertyName, string text, string[] items, int selectedIndex = -1, bool enabled = true, bool halfWidth = false) + { + if (Dictionary.ContainsKey(dataPropertyName)) + throw new DuplicateNameException(dataPropertyName + ": 已经存在"); + + EditInfo info = new EditInfo() + { + DataPropertyName = dataPropertyName, + EditType = EditType.Combobox, + Text = text, + Value = selectedIndex, + Enabled = enabled, + HalfWidth = halfWidth, + DataSource = items + }; + + Infos.Add(info); + Dictionary.TryAdd(info.DataPropertyName, info); + } } }