using NaiBa.Util.Extension; using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace TemuKJD.UserComponent { public partial class UcComboBoxMutiSelect : ComboBox { //private bool _IsMultiSelect; //public bool IsMultiSelect { get => _IsMultiSelect; set => _IsMultiSelect = value; } public string DefaultText { get; set; } = "全部"; public List SelectItems { get { if (this.checkedListBox == null) return this.Items.Cast().ToList(); List lstObj = new List(); for (int i = 0; i < checkedListBox.Items.Count; i++) { if (checkedListBox.GetItemChecked(i)) { lstObj.Add(checkedListBox.Items[i]); } } return lstObj; } } CheckedListBox checkedListBox = null; public UcComboBoxMutiSelect() { SetStyle(ControlStyles.DoubleBuffer | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true); UpdateStyles(); this.DropDown += UcComboBoxMutiSelect_DropDown; //this.DropDownClosed += UcComboBoxMutiSelect_DropDownClosed; } private void UcComboBoxMutiSelect_DropDownClosed(object sender, EventArgs e) { if (checkedListBox != null) checkedListBox.Visible = false; } private void UcComboBoxMutiSelect_DropDown(object sender, EventArgs e) { this.DropDownHeight = 1; if (checkedListBox == null) { checkedListBox = new CheckedListBox(); checkedListBox.Visible = false; checkedListBox.CheckOnClick = true; checkedListBox.ItemCheck += checkedListBox_ItemCheck; checkedListBox.MouseLeave += checkedListBox_MouseLeave; checkedListBox.MouseEnter += CheckedListBox_MouseEnter; checkedListBox.MouseMove += CheckedListBox_MouseMove; //checkedListBox.MouseUp += new MouseEventHandler(this.checkedListBox_MouseUp); Point point = new Point(); point.X = this.Left; point.Y = this.Top + this.Height; var parentForm = this.Parent; while (!(parentForm is Form)) { point.X += parentForm.Left; point.Y += parentForm.Top; parentForm = parentForm.Parent; } parentForm.Controls.Add(checkedListBox); checkedListBox.Location = point; checkedListBox.Size = new Size(Math.Max(80, this.Width), 100); } string str1 = string.Join(",", this.Items.Cast().Select(o => o.ParseToString())); string str2 = string.Join(",", this.checkedListBox.Items.Cast().Select(o => o.ParseToString())); if (str1 != str2) { checkedListBox.Items.Clear(); foreach (var item in this.Items) { checkedListBox.Items.Add(item.ParseToString(), true); } } this.FindForm().ActiveControl = checkedListBox; checkedListBox.Visible = true; checkedListBox.BringToFront(); } private void CheckedListBox_MouseMove(object sender, MouseEventArgs e) { //this.DropDownClosed -= UcComboBoxMutiSelect_DropDownClosed; } private void CheckedListBox_MouseEnter(object sender, EventArgs e) { //this.DropDownClosed -= UcComboBoxMutiSelect_DropDownClosed; } private void checkedListBox_MouseLeave(object sender, EventArgs e) { ////this.DropDownClosed += UcComboBoxMutiSelect_DropDownClosed; ////统计选取的项目 //string str = ""; //for (int i = 0; i < checkedListBox.Items.Count; i++) //{ // if (checkedListBox.GetItemChecked(i)) // { // str += $"{checkedListBox.Items[i].ParseToString()};"; // } //} //this.Text = str; checkedListBox.Visible = false; this.OnDropDownClosed(null); } private void checkedListBox_ItemCheck(object sender, EventArgs e) { //统计选取的项目 Task.Run(() => { Thread.Sleep(100); Invoke(new Action(() => { string str = ""; for (int i = 0; i < checkedListBox.Items.Count; i++) { if (checkedListBox.GetItemChecked(i)) { str += $"{checkedListBox.Items[i].ParseToString()};"; } } this.Text = str; string str1 = string.Join(";", this.Items.Cast().Select(o => o.ParseToString())); if (this.Text.Trim(';') == str1) { this.Text = this.DefaultText; } })); }); } } }