1. 程式人生 > >Combobox多選:與ListCheckbox組合【一】

Combobox多選:與ListCheckbox組合【一】

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace ComboBoxEdit
{
    public partial class ComboBoxEdit : ComboBox
    {
        public CheckedListBox list = new CheckedListBox();
        Dictionary<object, string> SelectItems = new Dictionary<object, string>();
        #region IsMultiSelect
        private bool isMultiSelect = false;
        [Description("設定是否允許多選")]
        public bool IsMultiSelect
        {
            get { return isMultiSelect; }
            set
            {
                isMultiSelect = value;
                if (isMultiSelect)
                {

                    //只有設定這個屬性為OwnerDrawFixed才可讓重畫起作用 
                    this.DrawMode = DrawMode.OwnerDrawFixed;
                    this.IntegralHeight = false;
                    this.DoubleBuffered = true;
                    this.DroppedDown = false;
                    this.DropDownHeight = 1;
                    this.DropDownStyle = ComboBoxStyle.DropDown;
                    list.CheckOnClick = true;
                    list.ItemCheck += new ItemCheckEventHandler(list_ItemCheck);
                    list.MouseUp += new MouseEventHandler(list_MouseUp);
                    list.MouseLeave += new EventHandler(list_MouseLeave);
                    list.BorderStyle = BorderStyle.Fixed3D;
                    list.Visible = false;
                }
                else
                {
                    //只有設定這個屬性為Normal才可讓重畫不起作用 
                    this.DrawMode = DrawMode.Normal;
                    this.IntegralHeight = true;
                    this.DoubleBuffered = true;
                    this.DropDownHeight = 106;
                }
            }
        }
        #endregion
        #region SelectedValues
        private string[] selectedValues = null;
        [Description("選擇項的Value值(陣列)")]
        public string[] SelectedValues
        {
            get
            {
                if (IsMultiSelect)
                {
                    string str = "";
                    foreach (KeyValuePair<object, string> m in SelectItems)
                    {
                        str = str + m.Key.ToString() + ";";
                    }
                    string[] array = str.Split(new char[1] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                    return array;
                } return selectedValues;
            }
            set
            {
                selectedValues = value;
            }
        }
        #endregion
        #region ItemCheck事件
        //設定CheckBoxList  
        private void list_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            if (e.NewValue == CheckState.Checked)
            {
                if (SelectItems.ContainsKey(list.SelectedValue))
                {
                    throw new Exception("Value具有重複的值!");
                }
                SelectItems.Add(list.SelectedValue, list.Text);
            }
            else if (e.NewValue == CheckState.Unchecked)
            {
                if (SelectItems.ContainsKey(list.SelectedValue))
                {
                    SelectItems.Remove(list.SelectedValue);
                }
            }
            else
            {
                if (e.NewValue == CheckState.Checked)
                {
                    if (SelectItems.ContainsKey(list.Text))
                    {
                        throw new Exception("手工新增的值中有重複的資料!");
                    }
                    SelectItems.Add(list.Text, list.Text);
                }
                else if (e.NewValue == CheckState.Unchecked)
                {
                    if (SelectItems.ContainsKey(list.Text))
                    {
                        SelectItems.Remove(list.Text);
                    }
                }
            }
        }   //設定ComboBox文字值  
        private void list_MouseUp(object sender, MouseEventArgs e)
        {
            string str = "";

            foreach (KeyValuePair<object, string> m in SelectItems)
            {
                str = str + m.Value + ";";
            }
            this.Text = str.Trim(';');
        }
        //滑鼠離開事件
        private void list_MouseLeave(object sender, EventArgs e)
        {
            list.Visible = false;
        }
        #endregion
        #region 重寫相關事件
        //點選滑鼠  
        protected override void OnMouseDown(MouseEventArgs e)
        {
            if (IsMultiSelect)
            {
                this.DroppedDown = false;
            }
        }
        //釋放滑鼠
        protected override void OnMouseUp(MouseEventArgs e)
        {
            if (IsMultiSelect)
            {
                this.DroppedDown = false;
            }
        }  //TextChange事件
        protected override void OnTextChanged(EventArgs e)
        {
            if (!list.Visible)
            {
                list.DataSource = null;
                list.Items.Clear();
                SelectItems.Clear();
            }
        }
        //下拉列表事件
        protected override void OnDropDown(EventArgs e)
        {
            if (IsMultiSelect)
            {
                list.Visible = !list.Visible;
                if (list.Visible)
                {
                    list.Focus();
                    list.ItemHeight = this.ItemHeight;
                    list.BorderStyle = BorderStyle.FixedSingle;
                    list.Font = this.Font;
                    list.Size = new Size(this.DropDownWidth, this.ItemHeight * (this.MaxDropDownItems - 1) - (int)this.ItemHeight / 2);
                    list.Location = new Point(this.Left, this.Top + this.ItemHeight + 6);
                    list.BeginUpdate();
                    if (this.DataSource != null)
                    {
                        list.DataSource = this.DataSource;
                        list.DisplayMember = this.DisplayMember;
                        list.ValueMember = this.ValueMember;
                    }
                    list.EndUpdate();
                    if (!this.Parent.Controls.Contains(list))
                    {
                        this.Parent.Controls.Add(list);
                        list.BringToFront();
                    }
                }
            }
        }
        #endregion
    }
}