CheckGroupBox Control

checkgroupbox

Have you ever needed a GroupBox with a internal CheckBox for enabling/disabling its contents readily draggable from your Visual Studio Toolbox?

Well, making this control is rather simple, and, indeed, many people should have done it already. But not a lot of them decided it was worth sharing their code with others. So this post is aimed at the very very beginner to C# and to System.Windows.Forms. But, if you are a newcomer to C# and just want its very best, I really advice you to take a look in Windows Presentation Foundation (WPF) rather than in Windows.Forms. Forms is better suited for lightweight or conventional applications needing a common interface, for those that can bear a lack of visual appeal in exchange for simplicity and fewer headaches with the user interface. Also if you plan to support older Windows versions or even other operating systems and architectures through Mono, then I fear you have no option but to use Windows.Forms.

using System;
using 
System.Drawing;
using 
System.ComponentModel;
using 
System.Windows.Forms;

namespace cSouza.Utils.WinForms
{

    /// <summary>
    ///   Enumerates possible actions to be performed by a
    ///   CheckGroupBox whenever its Check state changes.
    /// </summary>
    
public enum CheckGroupBoxCheckAction
    {
        None, Enable, Disable,
    }

    /// <summary>
    ///   Represents a Windows control that displays a frame around
    ///   a group of controls with an optional caption and checkbox.
    /// </summary>
    
public class CheckGroupBox : System.Windows.Forms.GroupBox
    {

        private CheckBox m_checkBox;
        private bool 
m_contentsEnabled = true;
        private 
CheckGroupBoxCheckAction m_checkAction =
            
CheckGroupBoxCheckAction.Enable;

        /// <summary>
        ///   Initializes a new instance of CheckGroupBox class.
        /// </summary>
        
public CheckGroupBox()
        {
            
this.SuspendLayout();

            this.m_checkBox = new CheckBox();
            this
.m_checkBox.AutoSize = true;
            this
.m_checkBox.Location = new Point(80);
            this
.m_checkBox.Padding = new Padding(3000);
            this
.m_checkBox.Checked = true;
            this
.m_checkBox.TextAlign ContentAlignment.MiddleLeft;
            this
.m_checkBox.CheckedChanged += new EventHandler(CheckBox_CheckedChanged);
            this
.Controls.Add(this.m_checkBox);

            this.ResumeLayout(true);
        
}

        #region Public Properties
        
/// <summary>
        /// Gets or sets a value indicating whether the
        /// CheckGroupBox is in the checked state.
        /// </summary>
        
[Category(“Appearance”)]
        [DefaultValue(
true)]
        
public bool Checked
        {
            
get return this.m_checkBox.Checked}
            
set this.m_checkBox.Checked = value; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether the controls
        /// contained inside this container can respond to user
        /// interaction.
        /// </summary>
        
[Category(“Behavior”)]
        [DefaultValue(
true)]
        
public bool ContentsEnabled
        {
            
get return this.m_contentsEnabled}
            
set
            
{
                
this.m_contentsEnabled = value;
                this
.OnContentsEnabledChanged(EventArgs.Empty);
            
}
        }

        /// <summary>
        /// The text associated with the control.
        /// </summary>
        
public override string Text
        {
            
get return this.m_checkBox.Text}
            
set this.m_checkBox.Text = value; }
        }

        /// <summary>
        /// Gets or sets a value indicating how a CheckGroupBox
        /// should behave when its CheckBox is in the checked state.
        /// </summary>
        
[Category(“Behavior”)]
        [DefaultValue(CheckGroupBoxCheckAction.Enable)]
        
public CheckGroupBoxCheckAction CheckAction
        {
            
get return this.m_checkAction}
            
set
            
{
                
this.m_checkAction = value;
                this
.OnCheckedChanged(EventArgs.Empty);
            
}
        }

        /// <summary>
        /// Gets the underlying CheckBox control contained
        /// in the CheckGroupBox control.
        /// </summary>
        
[Category(“Misc”)]
        
public CheckBox CheckBox
        {
            
get return this.m_checkBox}
        }
        
#endregion

        #region Event Handling
        
/// <summary>
        /// CheckGroupBox.CheckBox CheckedChanged event.
        /// </summary>
        /// <param name=”e”></param>
        
protected virtual void OnCheckedChanged(EventArgs e)
        {
            
if (this.m_checkAction !CheckGroupBoxCheckAction.None)
            {
                
// Toggle action depending on the value of checkAction.
                //   The ^ means a xor operation. The xor operation
                //   acts as a inversor, inverting the second operand
                //   whenever the first operand is true.

                this.ContentsEnabled =
                    
(this.m_checkAction == CheckGroupBoxCheckAction.Disable)
                     ^ 
this.m_checkBox.Checked;
            
}
        }

        /// <summary>
        /// ContentsEnabled Changed event.
        /// </summary>
        /// <param name=”e”></param>
        
protected virtual void OnContentsEnabledChanged(EventArgs e)
        {
            
this.SuspendLayout();
            foreach 
(Control control in this.Controls)
            {
                
if (control != this.m_checkBox)
                {
                    
// Set action for every control, except for
                    //  the CheckBox, which should remain intact.
                    
control.Enabled = this.m_contentsEnabled;
                
}
            }
            
this.ResumeLayout(true);
        
}

        private void CheckBox_CheckedChanged(object sender, EventArgs e)
        {
            
this.OnCheckedChanged(e);
        
}
        
#endregion

    }
}

As usual, just create a new empty class inside your project and copy & paste this code inside it. Press F6 to build your current solution and then you’ll be able to select CheckGroupBox using the Toolbox of the Visual Designer Editor of Visual Studio. Feel free to change the namespace name (or anything else) if you wish.

Leave a Reply

Your email address will not be published. Required fields are marked *