ComboBox复选

  • 格式:docx
  • 大小:23.88 KB
  • 文档页数:5

ComboBox复选功能 由于VS自带的控件ComboBox无法提供复选的功能,所以对ComboBox控件进行了重写,继承来自VS的ComboBox类,结合使用CheckListBox来实现复选的功能。 截图如下

完整代码如下 using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Windows.Forms;

namespace ComboBoxEdit { publicpartialclassComboBoxEdit : ComboBox {

publicCheckedListBox list = newCheckedListBox(); Dictionary SelectItems = newDictionary();

public ComboBoxEdit() { InitializeComponent(); }

#region IsMultiSelect privatebool isMultiSelect = false; [Description("设置是否允许多选")] publicbool 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 += newItemCheckEventHandler(list_ItemCheck); list.MouseUp += newMouseEventHandler(list_MouseUp); list.MouseLeave += newEventHandler(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 privatestring[] selectedValues=null; [Description("选择项的Value值(数组)")] publicstring [] SelectedValues { get { if (IsMultiSelect) { string str = ""; foreach (KeyValuePair m in SelectItems) { str = str + m.Key.ToString() + ";"; } string [] array=str.Split(newchar[1]{';'},StringSplitOptions.RemoveEmptyEntries); return array; } return selectedValues; } set { selectedValues = value; } } #endregion #region ItemCheck事件 //设置CheckBoxList privatevoid list_ItemCheck(object sender, ItemCheckEventArgs e) { if (e.NewValue == CheckState.Checked) { if (SelectItems.ContainsKey(list.SelectedValue)) { thrownewException("Value具有重复的值!"); } SelectItems.Add(list.SelectedValue, list.Text); } elseif (e.NewValue == CheckState.Unchecked) { if (SelectItems.ContainsKey(list.SelectedValue)) { SelectItems.Remove(list.SelectedValue); } } else { if (e.NewValue == CheckState.Checked) { if (SelectItems.ContainsKey(list.Text)) { thrownewException("手工添加的值中有重复的数据!"); }

SelectItems.Add(list.Text, list.Text); } elseif (e.NewValue == CheckState.Unchecked) { if (SelectItems.ContainsKey(list.Text)) { SelectItems.Remove(list.Text); } }

} }

//设置ComboBox文本值 privatevoid list_MouseUp(object sender, MouseEventArgs e) { string str = ""; foreach (KeyValuePair m in SelectItems) { str = str + m.Value + ";"; } this.Text = str.Trim(';'); } //鼠标离开事件 privatevoid list_MouseLeave(object sender, EventArgs e) { list.Visible = false; }

#endregion #region 重写相关事件 //点击鼠标 protectedoverridevoid OnMouseDown(MouseEventArgs e) { if (IsMultiSelect) { this.DroppedDown = false; } } //释放鼠标 protectedoverridevoid OnMouseUp(MouseEventArgs e) { if (IsMultiSelect) { this.DroppedDown = false; } } //TextChange事件 protectedoverridevoid OnTextChanged(EventArgs e) { if (!list.Visible) { list.DataSource = null; list.Items.Clear(); SelectItems.Clear(); } } //下拉列表事件 protectedoverridevoid 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 = newSize(this.DropDownWidth, this.ItemHeight * (this.MaxDropDownItems - 1) - (int)this.ItemHeight / 2); list.Location = newPoint(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 } }