Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleMake a GroupBox that includes a check box to enable and disable its controls in Visual Basic .NET
DescriptionThis example shows how to make a GroupBox that includes a check box to enable and disable its controls in Visual Basic .NET.
Keywordscontrol, custom control, GroupBox, CheckBox, VB.NET, Visual Basic .NET
CategoriesVB.NET, Controls
 
The CheckGroup control displays a check box with a caption in place of a normal GroupBox caption. When the user unchecks this box, the control disables all of the controls it contains.

The following code shows how the CheckGroup control works.

 
Public Class CheckGroup
    Inherits GroupBox

    Private WithEvents m_CheckBox As CheckBox

    ' Add the CheckBox to the control.
    Public Sub New()
        MyBase.New()

        m_CheckBox = New CheckBox
        m_CheckBox.Location = New Point(8, 0)
        Me.Text = "CheckGroup"

        Me.Controls.Add(m_CheckBox)
    End Sub

    ' Keep the CheckBox text synced with our text.
    Public Overrides Property Text() As String
        Get
            Return MyBase.Text
        End Get
        Set(ByVal Value As String)
            MyBase.Text = Value
            m_CheckBox.Text = Value

            Dim gr As Graphics = Me.CreateGraphics
            Dim s As SizeF = gr.MeasureString(MyBase.Text, _
                Me.Font)
            m_CheckBox.Size = New Size(s.Width + 20, _
                s.Height)
        End Set
    End Property

    ' Delegate to CheckBox.Checked.
    Public Property Checked() As Boolean
        Get
            Return m_CheckBox.Checked
        End Get
        Set(ByVal Value As Boolean)
            m_CheckBox.Checked = Value
        End Set
    End Property

    ' Enable/disable contained controls.
    Private Sub EnableDisableControls()
        For Each ctl As Control In Me.Controls
            If Not (ctl Is m_CheckBox) Then
                Try
                    ctl.Enabled = m_CheckBox.Checked
                Catch ex As Exception
                End Try
            End If
        Next ctl
    End Sub

    ' Enable/disable contained controls.
    Private Sub m_CheckBox_CheckedChanged(ByVal sender As _
        Object, ByVal e As System.EventArgs) Handles _
        m_CheckBox.CheckedChanged
        EnableDisableControls
    End Sub

    ' Enable/disable contained controls.
    ' We do this here to set editability
    ' when the control is first loaded.
    Private Sub m_CheckBox_Layout(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.LayoutEventArgs) _
        Handles m_CheckBox.Layout
        EnableDisableControls()
    End Sub
End Class
 
The control inherits from GroupBox so it can easily contain other controls. It uses the WithEvents keyword to declare a CheckBox variable so it can easily catch that control's events.

The control's constructor creates and positions the CheckBox control.

The control's Text property saves the new text in the CheckBox control and resizes that control so it can display the text.

The CheckGroup control delegates its Checked property to its CheckBox.

Helper subroutine EnableDisableControls enables or disables the controls contained within the CheckGroup depending on whether the CheckBox is checked. It does not disable the CheckBox itself so the user can check it later.

When the CheckBox is checked or unchecked, the CheckedChanged event handler calls EnableDisableControls.

Finally the control's Layout event handler also calls EnableDisableControls. When the control is first loaded, the CheckBox's CheckedChanged event handler fires before any contained controls are loaded so that event's call to EnableDisableControls does not enable and disable them properly. The control calls EnableDisableControls again in the Layout event handler after all of the contained controls are loaded.

 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated