Tuesday, March 6, 2012

Multiselect dropdown/combobox

In my current project i need a dropdown list where user can pick multiple items and then, after double-clickin on any of selected items, all of them appears in the tex area of dropdown. There is no standard controls that are offering this functionality, so i did it by hand. It's not something uniqe, i saw some samples on the web, but thi one is done using Infragistics controls for Windows forms and is relatively easy to implement. So here is the code (just one file)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Infragistics.Win;
using Infragistics.Win.UltraWinEditors;
using Infragistics.Win.UltraWinListView;

namespace MyControls
{
    public partial class MultiSelectComboBox : UltraTextEditor
    {
        private UltraListView lv;


        public MultiSelectComboBox()
        {
            InitializeComponent();
            SetTextEditorProperties();
        }

        public MultiSelectComboBox(IContainer container)
        {
            container.Add(this);
            InitializeComponent();
            SetTextEditorProperties();
        }

        public UltraListViewItemsCollection ListItems
        {
            get { return lv.Items; }
        }

        private void SetTextEditorProperties()
        {
            DropDownEditorButton button = new DropDownEditorButton();
            lv = new UltraListView();
            lv.AutoSize = false;
            lv.Width = Width;
            lv.View = UltraListViewStyle.List;
            lv.ViewSettingsList.ImageSize = Size.Empty;
            lv.ViewSettingsList.ColumnWidth = Width;
            lv.ViewSettingsList.MultiColumn = false;
            lv.DoubleClick += lv_DoubleClick;
            lv.Width = Size.Width;
            button.ButtonStyle = Infragistics.Win.UIElementButtonStyle.Office2010ScrollbarButton;
            button.Control = lv;
            ButtonsRight.Add(button);
            Enabled = true;
            UseFlatMode = DefaultableBoolean.True;
        }

        private void lv_DoubleClick(object sender, EventArgs e)
        {
            UltraListView listView = sender as UltraListView;
            if (listView == null)
            {
                return;
            }
            string result = string.Empty;
            foreach (var item in listView.Items)
            {
                if (!string.IsNullOrEmpty(item.Text) && item.IsSelected)
                {
                    result += item.Text + ";";
                }
            }
            Text = result.Remove(result.Length - 1, 1);
            DropDownEditorButton btn = ButtonsRight[0] as DropDownEditorButton;
            if (btn != null)
            {
                btn.CloseUp();
            }
        }

        #region Methods

        public void LoadControl(List<string> items)
        {
            lv.Items.Clear();
            lv.Width = Size.Width;
            Enabled = false;
            if (items == null)
            {
                return;
            }
            if (items.Count == 0)
            {
                return;
            }
            Enabled = true;
            try
            {
                lv.Items.Add(new UltraListViewItem(string.Empty));
                foreach (var item in items)
                {
                    if (!lv.Items.Exists(item))
                    {
                        lv.Items.Add(item, item);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public void LoadControl(object[] items)
        {
            try
            {
                List<string> list = new List<string>();
                list.AddRange(items.Select(t => t.ToString()));
                LoadControl(list);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #endregion
    }
}

No comments: