簡體   English   中英

基於 CheckBox 狀態禁用 ComboBox 項

[英]Disabling a ComboBox Item based on a CheckBox state

我有一個場景,我需要根據 CheckBox 的狀態禁用 ComboBox 的特定項目。

我在 ComboBox 中有五個復選框和五個項目。

如果未選中CheckBox1 ,則應禁用Item1 ,這同樣適用於所有其他 ComboBox 項。
我嘗試使用此代碼,但沒有成功:

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};

但是當我運行應用程序時,這些事件不會觸發。

如果將 CheckBox 添加到集合中,您可能會讓您的生活更輕松(並且代碼更易於管理),因此您可以使用配對的ComboBox 項的索引驗證從集合中檢索它的 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 項目啟用禁用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM