Laden...

ComboBoxMultiSelect

Erstellt von Andreas@Tricept vor 14 Jahren Letzter Beitrag vor 14 Jahren 3.884 Views
A
Andreas@Tricept Themenstarter:in
289 Beiträge seit 2006
vor 14 Jahren
ComboBoxMultiSelect

Hier der Gesamte Code der ComboBox mit Multiselect:

    public class ComboBoxMultiSelect : ComboBox
    {
        public bool MultiSelect { get; set; }
        int _checkWidth = 20;

        public object[] SlectedItems
        {
            get
            {
                RefreshCheckedArray();
                List<object> result = new List<object>();
                for (int i = 0; i < iChecked.Length; i++)
                {
                    if (iChecked[i])
                        result.Add(Items[i]);
                }
                return result.ToArray();
            }
        }
        public void SetChecked(int pIndex, bool pChecked)
        {
            RefreshCheckedArray();
            iChecked[pIndex] = pChecked;
        }

        bool[] iChecked = new bool[0];

        Dictionary<object, Color> iFontColors;

        public ComboBoxMultiSelect()
        {
            DrawMode = DrawMode.OwnerDrawVariable;
            iFontColors = new Dictionary<object, Color>();
            DropDownStyle = ComboBoxStyle.DropDownList;
            //this.OnCausesValidationChanged
 
        }

        protected override void OnCausesValidationChanged(EventArgs e)
        {
            base.OnCausesValidationChanged(e);
        }
        protected override void SetItemCore(int index, object value)
        {
            base.SetItemCore(index, value);
        }

        protected void RefreshCheckedArray()
        {
            bool[] temp = new bool[Items.Count];
            for (int i = 0; i < temp.Length; i++)
            {
                if (iChecked.Length > i)
                    temp[i] = iChecked[i];
                else
                    temp[i] = false;
            }
            iChecked = temp;
        }

     

        bool dropDown;
        protected override void OnDropDown(EventArgs e)
        {
            base.OnDropDown(e);
            RefreshCheckedArray();
            dropDown = true;
        }

        protected override void OnDropDownClosed(EventArgs e)
        {
            base.OnDropDownClosed(e);
            dropDown = false;
        }

        internal Rectangle GetParentBounds()
        {
            if (Parent == null)
                return Rectangle.Empty;
            int headerbreite = SystemInformation.ToolWindowCaptionHeight;
            Rectangle result = new Rectangle(Parent.Bounds.X + SystemInformation.FrameBorderSize.Width, Parent.Bounds.Y + headerbreite - SystemInformation.FrameBorderSize.Height, Parent.Width, Parent.Height);
            Control p = Parent.Parent;
            while (p != null)
            {
                result = new Rectangle(result.X + p.Bounds.X, result.Y + p.Bounds.Y, result.Width, result.Height);
                p = p.Parent;
            }
            return result;
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (e.KeyValue == 40 || e.KeyValue == 38)
                e.Handled = true;
            base.OnKeyDown(e);

        }

        

        private string GetText()
        {
            string text = "";
            for (int i = 0; i < iChecked.Length; i++)
            {
                if (iChecked[i])
                {
                    if (!string.IsNullOrEmpty(text))
                        text += " | ";
                    text += GetText(Items[i]);
                }
            }
            return text;
        }

        protected override void DefWndProc(ref Message m)
        {
            if (dropDown && m.Msg == 0x111)
            {
                Point p = Cursor.Position;
                int h = p.Y - (Bounds.Y + GetParentBounds().Y + Height + 19);
                int y = 0;
                for (int i = 0; i < Items.Count; i++)
                {
                    y += GetItemHeight(i);
                    if (h < y)
                    {
                        iChecked[i] = !iChecked[i];
                        RefreshItems();
                        break;
                    }
                }
                return;
            }
            base.DefWndProc(ref m);
        }

        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            base.OnDrawItem(e);
            if (e.Index < 0 || e.Index >= Items.Count)
                return;
            e.DrawBackground();
            if ((e.State & DrawItemState.Focus) != 0)
                e.DrawFocusRectangle();

            Brush b = null;
            object item = Items[e.Index];
            try
            {
                Color color = SystemColors.ControlText;
                Color colorOperation = SystemColors.Highlight;

                if ((e.State & DrawItemState.Focus) == DrawItemState.Focus)
                {
                    color = SystemColors.HighlightText;
                    colorOperation = SystemColors.HighlightText;
                }
                b = new SolidBrush(color);
                if(iFontColors.ContainsKey(item))
                    b = new SolidBrush(colorOperation);
                Rectangle textRect = e.Bounds;
                if (MultiSelect)
                {
                    if ((e.State & DrawItemState.ComboBoxEdit) == DrawItemState.ComboBoxEdit)
                    {
                        string text = GetText();
                        e.Graphics.DrawString(text, e.Font, b, e.Bounds);
                    }
                    else
                    {
                        ControlPaint.DrawCheckBox(e.Graphics, new Rectangle(textRect.X, textRect.Y, _checkWidth, textRect.Height), iChecked[e.Index] ? ButtonState.Checked : ButtonState.Normal);
                        textRect.Offset(_checkWidth, 0);
                        e.Graphics.DrawString(GetText(item), e.Font, b, textRect);
                    }
                }else
                    e.Graphics.DrawString(GetText(item), e.Font, b, textRect);
            }
            finally
            {
                if (b != null)
                    b.Dispose();
                b = null;
            }
        }

        private string GetText(object item)
        {
            if (item is DataRow && ((DataRow)item).Table != null && ((DataRow)item).Table.Columns.Contains(DisplayMember) && !((DataRow)item).IsNull(DisplayMember))
                return ((DataRow)item)[DisplayMember].ToString();
            else
                return item.ToString();
        }


    }
4.939 Beiträge seit 2008
vor 14 Jahren

Super Komponente!

Nur


public object[] SlectedItems

solltest du vllt. korrigieren -)