根据C#中的复选框状态禁用组合框项目

时间:2019-07-11 10:48:16

标签: c# winforms checkbox combobox

我是c#winforms的新手。我有一种情况,我需要根据复选框状态禁用组合框的特定项目。

我有五个复选框,即复选框1,复选框2,复选框3,复选框4,复选框5,以及组合框中的五个项目,即item1,item2,item3,item4,item5。

如果未选中复选框1,则应禁用项目1,所有其他组合框项目也应如此。我正在尝试这段代码,但未能实现我想要的功能,有人可以帮助我吗?谢谢。

private void ComboBox_SelectNode_DrawItem(object sender, DrawItemEventArgs e)
        {
            ComboBox comboBox = (ComboBox)sender;
            if (e.Index == 0)
            {
                if (this.isNode0Disabled)
                {
                    e.Graphics.DrawString(this.ComboBox_SelectNode.Items[e.Index].ToString(), this.myFont, Brushes.LightGray, e.Bounds);
                }
                else
                {
                    e.DrawBackground();
                    e.Graphics.DrawString(this.ComboBox_SelectNode.Items[e.Index].ToString(), this.myFont, Brushes.Black, e.Bounds);
                    e.DrawFocusRectangle();
                }

            }

            if (e.Index == 1)
            {
                if (this.isNode1Disabled)
                {
                    e.Graphics.DrawString(this.ComboBox_SelectNode.Items[e.Index].ToString(), this.myFont, Brushes.LightGray, e.Bounds);
                }
                else
                {
                    e.DrawBackground();
                    e.Graphics.DrawString(this.ComboBox_SelectNode.Items[e.Index].ToString(), this.myFont, Brushes.Black, e.Bounds);
                    e.DrawFocusRectangle();
                }

            }

            if (e.Index == 2)
            {
                if (this.isNode2Disabled)
                {
                    e.Graphics.DrawString(this.ComboBox_SelectNode.Items[e.Index].ToString(), this.myFont, Brushes.LightGray, e.Bounds);
                }
                else
                {
                    e.DrawBackground();
                    e.Graphics.DrawString(this.ComboBox_SelectNode.Items[e.Index].ToString(), this.myFont, Brushes.Black, e.Bounds);
                    e.DrawFocusRectangle();
                }

            }

            if (e.Index == 3)
            {
                if (this.isNode3Disabled)
                {
                    e.Graphics.DrawString(this.ComboBox_SelectNode.Items[e.Index].ToString(), this.myFont, Brushes.LightGray, e.Bounds);
                }
                else
                {
                    e.DrawBackground();
                    e.Graphics.DrawString(this.ComboBox_SelectNode.Items[e.Index].ToString(), this.myFont, Brushes.Black, e.Bounds);
                    e.DrawFocusRectangle();
                }

            }

            if (e.Index == 4)
            {
                if (this.isNode4Disabled)
                {
                    e.Graphics.DrawString(this.ComboBox_SelectNode.Items[e.Index].ToString(), this.myFont, Brushes.LightGray, e.Bounds);
                }
                else
                {
                    e.DrawBackground();
                    e.Graphics.DrawString(this.ComboBox_SelectNode.Items[e.Index].ToString(), this.myFont, Brushes.Black, e.Bounds);
                    e.DrawFocusRectangle();
                }

            }


        }

private void ComboBox_SelectNode_SelectedIndexChanged(object sender, EventArgs e)
        {
                if (this.ComboBox_SelectNode.SelectedIndex == 0)
                {
                    if (this.isNode0Disabled)
                    {
                        this.ComboBox_SelectNode.SelectedIndex = -1;
                    }
                }

                if (this.ComboBox_SelectNode.SelectedIndex == 1)
                {
                    if (this.isNode1Disabled)
                    {
                        this.ComboBox_SelectNode.SelectedIndex = -1;
                    }
                }

                if (this.ComboBox_SelectNode.SelectedIndex == 2)
                {
                    if (this.isNode2Disabled)
                    {
                        this.ComboBox_SelectNode.SelectedIndex = -1;
                    }
                }

                if (this.ComboBox_SelectNode.SelectedIndex == 3)
                {
                    if (this.isNode3Disabled)
                    {
                        this.ComboBox_SelectNode.SelectedIndex = -1;
                    }
                }

                if (this.ComboBox_SelectNode.SelectedIndex == 4)
                {
                    if (this.isNode4Disabled)
                    {
                        this.ComboBox_SelectNode.SelectedIndex = -1;
                    }
                }
          }

    private void CheckBox_Node0_CheckedChanged(object sender, EventArgs e)
    {

                if (this.checkBox_Node0.Checked == true)
                {
                    this.isNode0Disabled = false;
                }

                if (this.checkBox_Node0.Checked == false)
                {
                    this.isNode0Disabled = true;
                }

     }

修改后的代码:

 private void ComboBox_SelectNode_DrawItem(object sender, DrawItemEventArgs e)
        {
            TextFormatFlags comboTRFlags = TextFormatFlags.Left | TextFormatFlags.VerticalCenter;
            if (e.Index < 0)
            {
                return;
            }

            var combo = sender as ComboBox;
            bool isCheckBoxChecked = this.comboCheckBoxes[e.Index].Checked;
            if (isCheckBoxChecked)
            {
                e.DrawBackground();
            }
            else
            {
                using (var brush = new SolidBrush(combo.BackColor))
                {
                    e.Graphics.FillRectangle(brush, e.Bounds);
                }
            }

            string itemText = combo.GetItemText(combo.Items[e.Index]);
            Color textColor = isCheckBoxChecked ? e.ForeColor : SystemColors.GrayText;
            TextRenderer.DrawText(e.Graphics, itemText, combo.Font, e.Bounds, textColor, comboTRFlags);
        }

        private void ComboBox_SelectNode_MeasureItem(object sender, MeasureItemEventArgs e)
        => e.ItemHeight = this.ComboBox_SelectNode.Font.Height + 4;

并声明事件如下:

this.ComboBox_SelectNode.DrawItem += new DrawItemEventHandler(ComboBox_SelectNode_DrawItem);
            this.ComboBox_SelectNode.MeasureItem += new MeasureItemEventHandler(ComboBox_SelectNode_MeasureItem);
private CheckBox[] comboCheckBoxes;
this.comboCheckBoxes = new[] {this.checkBox_Node0, this.checkBox_Node1, this.checkBox_Node2, this.checkBox_Node3 , this.checkBox_Node4};

但是当我运行应用程序时,这些事件没有触发。

1 个答案:

答案 0 :(得分:1)

如果将CheckBox添加到集合中,可能会使您的生活更轻松(代码也更易于管理),因此可以使用配对组合框项目。
使用CheckBox控件数组,就像这样:
(当然,请根据您的设计调整CheckBoxes的名称)

private CheckBox[] comboCheckBoxes;
public form1()
{
    InitializeComponent();
    comboCheckBoxes = new[] { chkFirst, chkSecond, chkThird, chkFourth, chkLast};
}

然后可以在绘制每个项目的文本之前选择其样式。

我正在使用TextRenderer.DrawText来绘制项目的文本:使用此方法而不是Graphics.DrawString()可以正确呈现文本对齐方式。

TextFormatFlags comboTRFlags = TextFormatFlags.Left | TextFormatFlags.VerticalCenter;

private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
    if (e.Index < 0) return;
    var combo = sender as ComboBox;
    bool isCheckBoxChecked = comboCheckBoxes[e.Index].Checked;
    if (isCheckBoxChecked) {
        e.DrawBackground();
    }
    else {
        using (var brush = new SolidBrush(combo.BackColor)) {
            e.Graphics.FillRectangle(brush, e.Bounds);
        }
    }
    string itemText = combo.GetItemText(combo.Items[e.Index]);
    Color textColor = isCheckBoxChecked ? e.ForeColor : SystemColors.GrayText;
    TextRenderer.DrawText(e.Graphics, itemText, combo.Font, e.Bounds, textColor, comboTRFlags);
}

private void comboBox1_MeasureItem(object sender, MeasureItemEventArgs e) 
    => e.ItemHeight = comboBox1.Font.Height + 4;

private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
{
    if (!comboCheckBoxes[comboBox1.SelectedIndex].Checked) {
        comboBox1.SelectedIndex = -1;
        return;
    }
}

这是它的工作方式:

CheckBox ComboBox Item enable disable