WinForm 多选下拉框

 

效果图:WinForm 多选下拉框_第1张图片

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MultiCombox
{

    class MultiCombox
    {
        private DataGridView dgv;
        private Panel panel;
        private DataGridViewCheckBoxColumn colCheckbox = new DataGridViewCheckBoxColumn();
        private DataGridViewTextBoxColumn colText = new DataGridViewTextBoxColumn();
        private DataGridViewTextBoxColumn colValue = new DataGridViewTextBoxColumn();
        private Form form;
        private Control targetBox;
        public string realValue;
        public List displayMember;
        public List valueMember;
        private bool isOpen = false;
        private Control button;

        private string splitChar = ",";

        /// 
        /// 根据传入值初始化
        /// 
        /// 父窗体
        /// 目标文本框
        /// 显示的文本集合
        /// 实际值文本集合
        public MultiCombox(Form form,Control targetBox,List displayMember, List valueMember,Control button)
        {
            this.form = form;
            this.targetBox = targetBox;
            this.displayMember = displayMember;
            this.valueMember = valueMember;
            this.button = button;

            this.button.Click += button_Click;

            if (displayMember.Count != valueMember.Count)
            {
                throw new Exception("displayMember和valueMember项数不一致");
            }
            InitControl();
        }

        void button_Click(object sender, EventArgs e)
        {
            if (!isOpen)
                Open();
            else
                Close();
        }

        /// 
        /// 初始化控件
        /// 
        private void InitControl()
        {
            panel = new Panel();

            panel.BackColor = form.BackColor;
            panel.AutoScroll = true;
            panel.AutoSize = false;

            dgv = new DataGridView();

            // 
            // colCheckbox
            // 
            colCheckbox.HeaderText = "checkBox";
            colCheckbox.Name = "colCheckbox";
            colCheckbox.ReadOnly = true;
            colCheckbox.Width = 50;
            // 
            // colText
            // 
            colText.HeaderText = "text";
            colText.Name = "colText";
            colText.ReadOnly = true;
            // 
            // colValue
            // 
            colValue.HeaderText = "value";
            colValue.Name = "colValue";
            colValue.ReadOnly = true;
            colValue.Visible = false;

            dgv.AllowUserToResizeColumns = false;
            dgv.AllowUserToResizeRows = false;
            //dgv.AutoSizeRowsMode = System.Windows.Forms.DataGridViewAutoSizeRowsMode.DisplayedCells;
            dgv.BackgroundColor = System.Drawing.SystemColors.HighlightText;
            dgv.BorderStyle = System.Windows.Forms.BorderStyle.None;
            dgv.CellBorderStyle = System.Windows.Forms.DataGridViewCellBorderStyle.None;
            dgv.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            dgv.ColumnHeadersVisible = false;
            dgv.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            colCheckbox,
            colText,
            colValue});
            dgv.Dock = System.Windows.Forms.DockStyle.Fill;
            dgv.Location = new System.Drawing.Point(0, 0);
            dgv.MultiSelect = false;
            dgv.Name = "dgv";
            dgv.RowHeadersVisible = false;
            dgv.RowTemplate.Height = 23;
            dgv.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
            dgv.Size = new System.Drawing.Size(284, 261);
            dgv.TabIndex = 0;
            dgv.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(dgv_CellClick);

            dgv.Dock = DockStyle.Fill;
            panel.Controls.Add(dgv);
            form.Controls.Add(panel);
            panel.BringToFront();
            panel.Width = targetBox.Width;

            panel.Location = new Point(targetBox.Location.X, targetBox.Location.Y + targetBox.Height);

            dgv.Rows.Clear();

            for (int i = 0; i < displayMember.Count; i++)
            {
                dgv.Rows.Add(false, displayMember[i], valueMember[i]);
            }

            panel.Visible = false;

            panel.Leave += panel_Leave;
            
        }

        /// 
        /// 失去焦点时关闭窗口
        /// 
        /// 
        /// 
        private void panel_Leave(object sender, EventArgs e)
        {
            if ((sender as Control).Name == button.Name)
                return;
            else
                button_Click(null, null);
        }

        private void dgv_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            var dgv = sender as DataGridView;
            dgv.Rows[e.RowIndex].Selected = true;
            dgv.Rows[e.RowIndex].Cells[0].Value = !Convert.ToBoolean(dgv.Rows[e.RowIndex].Cells[0].Value);
        }

        /// 
        /// 下拉框是否已打开
        /// 
        /// 
        public bool IsOpen()
        {
            return isOpen;
        }


        /// 
        /// 设置分割的标签
        /// 
        /// 
        public void SetSplitChar(string splitChar)
        {
            this.splitChar = splitChar;
        }


        /// 
        /// 获取分割的标签
        /// 
        /// 
        public string GetSplitChar()
        {
            return splitChar;
        }


        /// 
        /// 打开下拉框
        /// 
        public void Open()
        {
            if (isOpen)
                return;

            targetBox.Text = "";
            realValue = "";
            panel.Visible = true;
            isOpen = true;
        }

        /// 
        /// 关闭下拉框
        /// 
        public void Close()
        {
            if (!isOpen)
                return;

            panel.Visible = false;
            isOpen = false;

            foreach (DataGridViewRow row in dgv.Rows)
            {
                if (Convert.ToBoolean(row.Cells[colCheckbox.Name].Value))
                {
                    targetBox.Text += row.Cells[colText.Name].Value + splitChar;
                    realValue += row.Cells[colValue.Name].Value + splitChar;
                }
            }
            targetBox.Text = targetBox.Text.TrimEnd(splitChar.ToArray());
            realValue = realValue.TrimEnd(splitChar.ToArray());
        }
    }
}

 

 

 

 

你可能感兴趣的:(学习)