jeudi 11 juillet 2019

Disabling the Combo Box Item based on the Check Box state in C#

I am new to c# winforms. I have a scenario where i need to disable the specific items of a combo box based on the check Box state.

I have five check boxes namely check box 1, check box 2, check box 3, check box 4,check box 5 and five items in a combo box namely item1, item2, item3, item4, item5.

If check box 1 is unchecked, item 1 should be disabled and the same applies for all the other combo box items. I am trying this code, but was unsuccessful in achieving what i want, can any one help me? Thanks.

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









Aucun commentaire:

Enregistrer un commentaire