在WinForms中实现Checked Combobox的简单方法是什么?

11

有没有人知道在WinForms中简单实现带选框的下拉列表?我在谷歌搜索时没有找到任何东西。

我想要像“Windows计划任务触发器编辑”这样的行为:

enter image description here

2个回答

14

这是你想要的,我自己编写了这个代码。
概念非常简单,使用一个面板并在其上添加复选框。然后强制组合框禁用其下拉菜单,但仍然可以点击其向下箭头。使用组合框的DropDownDropDownClosed事件。当你使组合框的下拉列表展开时,将面板的Visible属性设置为True,关闭组合框的下拉菜单时隐藏它。

//designer class

// 
// comboBox1
// 
this.comboBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
this.comboBox1.DropDownHeight = 1;
this.comboBox1.DropDownWidth = 1;
this.comboBox1.FormattingEnabled = true;
this.comboBox1.IntegralHeight = false;
this.comboBox1.Location = new System.Drawing.Point(256, 371);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(238, 21);
this.comboBox1.TabIndex = 5;
this.comboBox1.DropDown += new System.EventHandler(this.comboBox1_DropDown);
this.comboBox1.DropDownClosed += new System.EventHandler(this.comboBox1_DropDownClosed);
// 
// panel1
// 
this.panel1.BackColor = System.Drawing.Color.White;
this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.panel1.Controls.Add(this.checkBox9);
this.panel1.Controls.Add(this.checkBox8);
this.panel1.Controls.Add(this.checkBox7);
this.panel1.Controls.Add(this.checkBox6);
this.panel1.Controls.Add(this.checkBox5);
this.panel1.Controls.Add(this.checkBox4);
this.panel1.Controls.Add(this.checkBox3);
this.panel1.Controls.Add(this.checkBox2);
this.panel1.Controls.Add(this.checkBox1);
this.panel1.Location = new System.Drawing.Point(252, 394);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(245, 68);
this.panel1.TabIndex = 6;
// 
// checkBox9
// 
this.checkBox9.AutoSize = true;
this.checkBox9.Location = new System.Drawing.Point(162, 48);
this.checkBox9.Name = "checkBox9";
this.checkBox9.Size = new System.Drawing.Size(80, 17);
this.checkBox9.TabIndex = 9;
this.checkBox9.Text = "checkBox9";
this.checkBox9.UseVisualStyleBackColor = true;
// 
// checkBox8
// 
this.checkBox8.AutoSize = true;
this.checkBox8.Location = new System.Drawing.Point(162, 27);
this.checkBox8.Name = "checkBox8";
this.checkBox8.Size = new System.Drawing.Size(80, 17);
this.checkBox8.TabIndex = 8;
this.checkBox8.Text = "checkBox8";
this.checkBox8.UseVisualStyleBackColor = true;
// 
// checkBox7
// 
this.checkBox7.AutoSize = true;
this.checkBox7.Location = new System.Drawing.Point(162, 4);
this.checkBox7.Name = "checkBox7";
this.checkBox7.Size = new System.Drawing.Size(80, 17);
this.checkBox7.TabIndex = 7;
this.checkBox7.Text = "checkBox7";
this.checkBox7.UseVisualStyleBackColor = true;
// 
// checkBox6
// 
this.checkBox6.AutoSize = true;
this.checkBox6.Location = new System.Drawing.Point(82, 47);
this.checkBox6.Name = "checkBox6";
this.checkBox6.Size = new System.Drawing.Size(80, 17);
this.checkBox6.TabIndex = 5;
this.checkBox6.Text = "checkBox6";
this.checkBox6.UseVisualStyleBackColor = true;
// 
// checkBox5
// 
this.checkBox5.AutoSize = true;
this.checkBox5.Location = new System.Drawing.Point(82, 26);
this.checkBox5.Name = "checkBox5";
this.checkBox5.Size = new System.Drawing.Size(80, 17);
this.checkBox5.TabIndex = 4;
this.checkBox5.Text = "checkBox5";
this.checkBox5.UseVisualStyleBackColor = true;
// 
// checkBox4
// 
this.checkBox4.AutoSize = true;
this.checkBox4.Location = new System.Drawing.Point(82, 4);
this.checkBox4.Name = "checkBox4";
this.checkBox4.Size = new System.Drawing.Size(80, 17);
this.checkBox4.TabIndex = 3;
this.checkBox4.Text = "checkBox4";
this.checkBox4.UseVisualStyleBackColor = true;
// 
// checkBox3
// 
this.checkBox3.AutoSize = true;
this.checkBox3.Location = new System.Drawing.Point(3, 47);
this.checkBox3.Name = "checkBox3";
this.checkBox3.Size = new System.Drawing.Size(80, 17);
this.checkBox3.TabIndex = 2;
this.checkBox3.Text = "checkBox3";
this.checkBox3.UseVisualStyleBackColor = true;
// 
// checkBox2
// 
this.checkBox2.AutoSize = true;
this.checkBox2.Location = new System.Drawing.Point(4, 24);
this.checkBox2.Name = "checkBox2";
this.checkBox2.Size = new System.Drawing.Size(80, 17);
this.checkBox2.TabIndex = 1;
this.checkBox2.Text = "checkBox2";
this.checkBox2.UseVisualStyleBackColor = true;
// 
// checkBox1
// 
this.checkBox1.AutoSize = true;
this.checkBox1.Location = new System.Drawing.Point(4, 4);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(80, 17);
this.checkBox1.TabIndex = 0;
this.checkBox1.Text = "checkBox1";
this.checkBox1.UseVisualStyleBackColor = true;

public Form1()
{
    InitializeComponent();
    panel1.Visible =false;
}


private void comboBox1_DropDown(object sender, EventArgs e)
{
    panel1.Visible = true;
}

private void comboBox1_DropDownClosed(object sender, EventArgs e)
{
    panel1.Visible = false;
}

1
谢谢,我已经实现了与此非常相似的东西,尽管我使用了一个带有 CheckBoxes = TrueListView 而不是面板,这样我就不必担心对齐复选框的问题。 - Matt Wilko
2
如果我实现了这个功能,一旦我点击panel1,下拉菜单就会关闭,从而导致面板隐藏。 - Adnan Bhatti
我和 @MSStp 有同样的问题。 - Mattias Nordqvist
已经完成了,但是无法勾选复选框。由于复选框在勾选之前就关闭了,我该怎么办? - sindhu jampani
2
这不起作用,因为我们没有将面板添加到组合框下拉列表中,所以每当我们点击面板项时,组合框会认为我们在其外部单击,因此它会关闭面板(这是组合框的默认行为)。 - Digambar Malla
1
与其使用 DropDownDropDownClosed 事件,这会在单击复选框时立即使面板消失,我们可以使用 MouseClick 事件。 private void comboBox1_MouseClick(object sender, MouseEventArgs e) { comboBox1.DroppedDown = false; if (panel1.Visible) { panel1.Visible = false; } else { panel1.Visible = true; } } - Adarsh Ravi

2

从截图上看,似乎有两个控件,一个是组合框,另一个是复选框列表框。您可以使用组合框的DropDown方法使复选框列表框可见,然后一旦用户离开复选框列表框,将所选值作为文本附加到组合框中(然后使复选框列表框不可见)。这只是一个想法;我无法访问编辑器来查看是否可能,但这似乎很有可能。


是的,发布后我也有同样的想法 - 我认为这是我要采取的方法,并看看涉及多少工作。 - Matt Wilko

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接