您需要创建自己的用户控件。第一步是创建System.Windows.Forms.ComboBox 类的子类:
using System;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class CheckComboBox : ComboBox
{
public CheckComboBox()
{
this.DrawMode = DrawMode.OwnerDrawFixed;
}
}
}
您应该设置 DrawMode 属性来告诉 ComboBox 我们打算自己呈现下拉列表项。下一步是定义一个类来包含我们的下拉列表项数据并维护状态。这是一个简单的类:
namespace WindowsFormsApp1
{
public class CheckComboBoxItem
{
public CheckComboBoxItem(string text, bool initialCheckState)
{
_checkState = initialCheckState;
_text = text;
}
private bool _checkState = false;
public bool CheckState
{
get { return _checkState; }
set { _checkState = value; }
}
private string _text = "";
public string Text
{
get { return _text; }
set { _text = value; }
}
public override string ToString()
{
return "Select Options";
}
}
}
之后返回您的CheckComboBox.cs 并添加代表DrawItem 和SelectedIndexChanged 事件。
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
namespace WindowsFormsApp1
{
public partial class CheckComboBox : ComboBox
{
public event EventHandler CheckStateChanged;
public CheckComboBox()
{
this.DrawMode = DrawMode.OwnerDrawFixed;
this.DrawItem += new DrawItemEventHandler(CheckComboBox_DrawItem);
this.SelectedIndexChanged += new EventHandler(CheckComboBox_SelectedIndexChanged);
}
void CheckComboBox_DrawItem(object sender, DrawItemEventArgs e)
{
if (e.Index == -1)
{
return;
}
if (!(Items[e.Index] is CheckComboBoxItem))
{
e.Graphics.DrawString(
Items[e.Index].ToString(),
this.Font,
Brushes.Black,
new Point(e.Bounds.X, e.Bounds.Y));
return;
}
CheckComboBoxItem box = (CheckComboBoxItem)Items[e.Index];
CheckBoxRenderer.RenderMatchingApplicationState = true;
CheckBoxRenderer.DrawCheckBox(
e.Graphics,
new Point(e.Bounds.X, e.Bounds.Y),
e.Bounds,
box.Text,
this.Font,
(e.State & DrawItemState.Focus) == 0,
box.CheckState ? CheckBoxState.CheckedNormal :
CheckBoxState.UncheckedNormal);
}
void CheckComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
CheckComboBoxItem item = (CheckComboBoxItem)SelectedItem;
item.CheckState = !item.CheckState;
CheckStateChanged?.Invoke(item, e);
}
}
}
在 DrawItems 委托中,我们要做的第一件事是验证我们正在渲染的项目是否已添加为 CheckComboBoxItem。如果不是,我们将其渲染为一个简单的字符串。否则,我们从 Items 集合中获取适当的 CheckComboBoxItem(使用 DrawItemEventArgs.Index 属性)。然后我们调用 CheckBoxRenderer.DrawCheckBox() 方法,传入我们要在其中渲染 CheckBox 的 Graphics 对象,以及位置、大小、文本、字体、焦点和检查状态。
第二个允许我们切换下拉列表中的复选框,但不允许此控件的用户知道发生了任何事情。所以我们还添加了一个公共事件来通知控件的用户下拉列表中某个项目的检查状态发生了变化:
public event EventHandler CheckStateChanged;
最后,如果你想使用这个控件,在你的应用程序的默认 Form1 中输入这个代码:
using System;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
checkComboBox1.Items.Add(new CheckComboBoxItem("One", true));
checkComboBox1.Items.Add(new CheckComboBoxItem("Two", true));
checkComboBox1.Items.Add(new CheckComboBoxItem("Three", true));
this.checkComboBox1.CheckStateChanged += new EventHandler(this.checkComboBox1_CheckStateChanged);
}
private void checkComboBox1_CheckStateChanged(object sender, EventArgs e)
{
if (sender is CheckComboBoxItem)
{
CheckComboBoxItem item = (CheckComboBoxItem)sender;
}
}
}
}
您有很多对您有用的链接。你没有输入任何代码,所以我们不知道你到底需要什么......
https://www.codeproject.com/Articles/31105/A-ComboBox-with-a-CheckedListBox-as-a-Dropdown
https://www.codeproject.com/Articles/21085/CheckBox-ComboBox-Extending-the-ComboBox-Class-and
https://www.codeproject.com/Articles/18929/An-OwnerDraw-ComboBox-with-CheckBoxes-in-the-Drop
感谢@Rob 和@Mamun 纠正我。