将复选框添加到comboBox在下拉列表中不可见

时间:2017-12-21 07:32:01

标签: c# windows forms checkbox combobox

我正在为windowsform中的comboBox添加一个复选框。复选框添加,我可以选择它们作为项目,但我可以看到框的文本勾选。在组合框下拉列表中,项目列表为空,并在接受所选项目时单击它们以指定正确的详细信息。

如何让它们可见以查看方框和名称?

1 个答案:

答案 0 :(得分:0)

您需要创建自己的用户控件。第一步是创建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并添加代理DrawItemSelectedIndexChanged事件。

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()方法,传入Graphics对象,我们想要渲染CheckBox,以及位置,大小,文本,字体,焦点和检查状态。 第二个允许我们切换下拉菜单中的复选框,但不允许此控件的用户知道发生了任何事情。因此,我们还添加了一个公共事件,以通知控件的用户更改下拉列表中项目的检查状态:

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纠正我。