创建窗体,拖以下控件
效果:
鼠标点击comboBox箭头显示下拉框,鼠标移开下拉框隐藏,
可单选,全选,选中的数据显示在右边框,默认textBox为隐藏,当有选中的时候textBox显示
创建ComboBoxMul 类继承ComboBox
ComboBoxMutiSelect类 代码
class ComboBoxMutiSelect : ComboBox
{
private bool _IsMultiSelect;
public bool IsMultiSelect { get => _IsMultiSelect; set => _IsMultiSelect = value; }
public ComboBoxMutiSelect()
{
SetStyle(ControlStyles.DoubleBuffer |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.AllPaintingInWmPaint, true);
UpdateStyles();
}
}
打开对话框的设计代码:对话框名字.Desiger.cs
将
private System.Windows.Forms.ComboBox comboBox1;
改为
private ComboBoxMutiSelect comboBox1;
将
this.comboBox1 = new System.Windows.Forms.ComboBox();
改为
this.comboBox1 = new ProductWindowsMultiCombo/*命名空间名称*/.ComboBoxMutiSelect();
//修改comboBox1控件绘制代码
// comboBox1
this.comboBox1.DropDownHeight = 1;
this.comboBox1.IntegralHeight = false;
this.comboBox1.IsMultiSelect = false;
this.comboBox1.FormattingEnabled = true;
this.comboBox1.Location = new System.Drawing.Point(83, 5);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(79, 20);
this.comboBox1.TabIndex = 41;
打开窗体名.cs
窗体加载事件
.cs代码
private void Form1_Load(object sender, EventArgs e)
{
#region 初始化
this.comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
this.comboBox1.DropDownStyle = ComboBoxStyle.DropDown;
comboBox1.IsMultiSelect = true;
ListBoxPaint();
textBox1.Visible = false;
#endregion
}
private void ListBoxPaint()
{
#region 添加事件
checkedListBox1.BorderStyle = BorderStyle.Fixed3D;
checkedListBox1.Visible = false;
checkedListBox1.CheckOnClick = true;
checkedListBox1.ItemCheck += new ItemCheckEventHandler(this.checkedListBox1_ItemCheck_1);
checkedListBox1.MouseLeave += new EventHandler(this.checkedListBox1_MouseLeave_1);
#endregion
#region 給checkedListBox1添加數據
checkedListBox1.Items.Add("1");
checkedListBox1.Items.Add("2");
checkedListBox1.Items.Add("3");
checkedListBox1.Items.Add("4");
checkedListBox1.Items.Add("5");
#endregion
}
//多選事件
private void checkedListBox1_ItemCheck_1(object sender, EventArgs e)
{
if (comboBox1.IsMultiSelect == true)//如果需要多選
{
checkedListBox1.BeginUpdate();//開始繪製
//定位comBox的坐標
Point point = new Point();
point.X = comboBox1.Left;
point.Y = comboBox1.Bottom;
//繪製 checkedListBox
checkedListBox1.FormattingEnabled = true;
checkedListBox1.Location = point;//将checkedListBox定位到comboBox1的左下角
checkedListBox1.Name = "checkedListBox1";
checkedListBox1.TabIndex = 2;
checkedListBox1.Visible = true;//繪製完成後,屬性設置為可見
panel1.Controls.Add(checkedListBox1);//将控件掛載到父控件
checkedListBox1.EndUpdate();//停止绘制
}
}
//鼠標離開事件
private void checkedListBox1_MouseLeave_1(object sender, EventArgs e)
{
//統計選中的item
string str = "";
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
if (checkedListBox1.GetItemChecked(i))
{
str += $"{checkedListBox1.Items[i].ToString()};";
textBox1.Visible = true;
}
}
if (str == "")
{
textBox1.Visible = false;
}
//鼠標離開後,將內容顯示到控件
textBox1.Text = str;
comboBox1.Text = str;
checkedListBox1.Visible = false;
}
//combox點擊事件
private void comboBox1_Click(object sender, EventArgs e)
{
checkedListBox1.Visible = true;
}
//全選點擊事件
private void lbSelectAll_Click(object sender, EventArgs e)
{
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
checkedListBox1.SetItemChecked(i, true);
}
}
//取消點擊事件
private void lbSelectNo_Click(object sender, EventArgs e)
{
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
checkedListBox1.SetItemChecked(i, false);
}
}