Click here to Skip to main content
15,112,667 members
Articles / Programming Languages / Visual Basic
Posted 10 Apr 2006


36 bookmarked

EnumGroupBox - a semi-automatic GroupBox control

Rate me:
Please Sign up or sign in to vote.
4.20/5 (10 votes)
3 May 20063 min read
A GroupBox automatically filled using Enums as templates.

Sample Image


When working on a project to display and compare source codes, I often had to change my GUI. Inside the GUI, different flags, options etc., can be chosen to perform different process tasks. Whenever I added a new RadioButton or CheckBox, I had to redesign the GUI and write a lot of code (starting inside the GUI and finally ending inside the process).

Therefore, I decided to create a class which does most of this work automatically:

This EnumGroupBox class is inherited from a GroupBox, and contains some RadioButtons and CheckBoxes. The RadioButtons/CheckBoxes are created using an Enum as template. Every member of the Enum is represented by a RadioButton/CheckBox.

The EnumGroupBox class, in general, can be used for every application without changes. It fires an EnumChanged event to the parent form. The EnumGroupBox class itself is bound to a variable inside a different class, ProcessOptions, representing the process flags and the options of the individual application.

The EnumGroupBox properties the programmer can define are:

  • Size of the single controls.
  • Position of the upper left control inside the EnumGroupBox.
  • Row and column offsets of the other controls.
  • Whether to auto-title the EnumGroupBox with the name of the Enum.

Using the code

To use the code, there are four steps:

First, define the Enum and a corresponding property inside the ProcessOptions class, like:

public enum ESpecialSort
 NoSpecialSort = 0,
 ElseSort = 1,
 CaseSort = ElseSort << 1

private ESpecialSort m_SpecialSort;

public ESpecialSort SpecialSort
 return m_SpecialSort;
 m_SpecialSort = value;

Important: The enum name must be the property name preceded by an 'E'. The name of the corresponding local variable is arbitrary.

Second, inside your GUI, define a variable containing the process flags and options:

'create our process describing class
Private processType As New NSourceStructure.ProcessOptions

Third, inside your GUI designer, put an EnumGroupBox control onto your form. (In the sample, its name is 'egbSpecialSort'.)

In the Form's Load event, define for the EnumGroupBox control:

  • the ProcessOption Enum to be used here.
  • type of controls (RadioButton or CheckBox) like:
    ' the Windows Form Designer creates
    ' the EnumGroupBoxes and sets their location, size etc.
    ' we have to tell about the underlying enums
    ' first define the enum and the control type
    egpSpecialSort.EnumSource(processType.SpecialSort, ControlType.checkBox)

Fourth, define the actions for the EnumGroupBox EnumChanged event, like:

Private Sub egbSpecialSort_EnumChanged(ByVal sender As Object, _
  ByVal a As NEnumGroupBox.EnumGroupBox.CheckArgs) _
  Handles gbSpecialSort.EnumChanged

    ' because the EnumGroupBox does not know about the 'parent' Enum
    ' we have to set the 'parent' value here
    ' by passing the values to the process defining class
    processType.SpecialSort = CType(a.Value, _

    ' execute your special code using the new value
    lblSpecialSort.Text = CType(CType(a.Value, Int16), String)

    ' to check for a single flag use the code
    ' <processType>.Check(<flag>)
    ' The flag check may be anywhere in your program.
    ' it is here for example only
    If processType.Check(NSourceStructure.ProcessOptions.ESpecialSort.CaseSort) Then
    MsgBox("NSourceStructure.ProcessOptions.ESpecialSort.CaseSort is activated")
    End If
End Sub

Points of Interest

Currently, inside the EnumChanged event, the value of the corresponding ProcessOption has to be set. Would be nice if somebody finds a way to do this inside the EnumGroupBox class.

Demo Sample

The code is written under Visual Studio 2003, with the .NET Framework 1.1.

I tried:

  • It also can be compiled with Visual Studio 2005 and .NET Framework 2.0.
  • After converting the project, you just have to redefine some references. Source code changes are not necessary.

I preferred the 2003 version because I think that this is still the standard IDE of most CodeProject readers.

Like explained in the beginning, the demo sample contains the ProcessOptions class from a project, 'Source Explorer', I'm working on. So the names of the flags and options reflect a little bit the features of the final application. That project will be published here later. As writing articles for the CodeProject is not my primary profession, publishing of the final article may take a while. Sorry!


  • 07-Apr-2006 - First version.
  • 14-Apr-2006 - Second version:
    • Added 'AttributeDescription' feature as suggested by Uwe Keim.
    • Added 'AutoSize' feature as a 'Point of Interest' of the first version.
    • Added the ability to change some properties at runtime.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Peter Schlang
Web Developer
Germany Germany
Peter Schlang, working with computers since 1974
Developing mainly for newspapers since 1981, first as employee of ATEX
Freelancer since 1987
Preferred language is VB: Starting with VB 1.0 and VBDOS, up to VB.NET

Comments and Discussions

GeneralConfiguration via PropertyGrid Pin
Mark Belles26-Jun-06 16:21
MemberMark Belles26-Jun-06 16:21 
GeneralSame idea / different control Pin
OrlandoCurioso22-Apr-06 6:20
MemberOrlandoCurioso22-Apr-06 6:20 
GeneralVery nice Pin
jpcano11-Apr-06 4:45
Memberjpcano11-Apr-06 4:45 
GeneralRe: Very nice Pin
Peter Schlang11-Apr-06 4:59
MemberPeter Schlang11-Apr-06 4:59 
GeneralRe: Very nice Pin
jpcano11-Apr-06 5:35
Memberjpcano11-Apr-06 5:35 
GeneralRe: Very nice Pin
Peter Schlang11-Apr-06 7:20
MemberPeter Schlang11-Apr-06 7:20 
GeneralEnum-description Pin
Uwe Keim10-Apr-06 20:48
sitebuilderUwe Keim10-Apr-06 20:48 
GeneralRe: Enum-description Pin
Peter Schlang10-Apr-06 22:23
MemberPeter Schlang10-Apr-06 22:23 
GeneralRe: Enum-description Pin
Uwe Keim10-Apr-06 22:26
sitebuilderUwe Keim10-Apr-06 22:26 
GeneralRe: Enum-description Pin
Peter Schlang11-Apr-06 5:04
MemberPeter Schlang11-Apr-06 5:04 
GeneralRe: Enum-description Pin
Uwe Keim11-Apr-06 19:48
sitebuilderUwe Keim11-Apr-06 19:48 
GeneralRe: Enum-description Pin
Peter Schlang15-Apr-06 2:05
MemberPeter Schlang15-Apr-06 2:05 
GeneralRe: Enum-description Pin
Uwe Keim15-Apr-06 2:33
sitebuilderUwe Keim15-Apr-06 2:33 
GeneralNice! Pin
Marc Clifton10-Apr-06 12:57
mvaMarc Clifton10-Apr-06 12:57 
GeneralRe: Nice! Pin
Peter Schlang11-Apr-06 5:12
MemberPeter Schlang11-Apr-06 5:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.