locked
I have multiple check boxes(18) and want to be able to change backcolor , forecolor based on checkbox name RRS feed

  • Question

  • In C# on windows form I have 18 checkboxes.  When clicked I want to change flatstyle to popup and also change backcolor and forecolor.  When unclicked want to change back.  So I want a common class that I can pass checkbox name to and have that class change the properties based on current setting.  
    Friday, November 27, 2020 7:29 PM

All replies

  • Hello,

    Here is a code sample that provides basic logic for changing colors and Flat style along with a crude example for by control names.

    The following class is a tad overkill for this task but can be very useful when there is a need to find and work with controls that are in child containers like panels and group boxes (which you may or may not be using here). Note the method DescendantsOfCheckBoxes, you can use other ones too as found in my GitHub Gist.

    public static class GenericExtensions
    {
        /// <summary>
        /// Get a collection of a specific type of control from a control or form.
        /// </summary>
        /// <typeparam name="T">Type of control</typeparam>
        /// <param name="control">Control to traverse</param>
        /// <returns>IEnumerable of T</returns>
        public static IEnumerable<T> Descendants<T>(this Control control) where T : class
        {
            foreach (Control child in control.Controls)
            {
                T thisControl = child as T;
                if (thisControl != null)
                {
                    yield return (T)thisControl;
                }
    
                if (child.HasChildren)
                {
                    foreach (T descendant in Descendants<T>(child))
                    {
                        yield return descendant;
                    }
                }
            }
        }
        public static List<CheckBox> DescendantsOfCheckBoxes(this Control sender)
        {
            return sender.Descendants<CheckBox>().ToList();
        }
    }

    Form code

    public partial class Form1 : Form
    {
        private List<CheckBox> _checkBoxes = new List<CheckBox>();
        public Form1()
        {
            InitializeComponent();
    
            Shown += Form1_Shown;
        }
    
        private void Form1_Shown(object sender, EventArgs e)
        {
            _checkBoxes = this.DescendantsOfCheckBoxes();
            
            _checkBoxes.ForEach(cb => 
            {
                cb.CheckedChanged += CheckedChanged;
                cb.FlatStyle = FlatStyle.Popup;
            });
        }
    
        private void CheckedChanged(object sender, EventArgs e)
        {
            var cb = (CheckBox) sender;
            var number = Convert.ToInt32(Regex.Replace(cb.Name, "[^0-9 _]", ""));
    
            if (number > 5)
            {
                if (cb.FlatStyle == FlatStyle.Popup)
                {
                    cb.FlatStyle = FlatStyle.Flat;
    
                    cb.ForeColor = Color.Crimson;
                    cb.BackColor = Color.AntiqueWhite;
                }
                else
                {
                    cb.FlatStyle = FlatStyle.Popup;
                    cb.ForeColor = SystemColors.ControlText;
                    cb.BackColor = SystemColors.Control;
                }
            }
    
        }
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    My GitHub code samples
    GitHub page

    Check out:  the new Microsoft Q&A forums

    Friday, November 27, 2020 11:08 PM