Click here to Skip to main content
Click here to Skip to main content

EnumGroupBox - a semi-automatic GroupBox control

, 3 May 2006
Rate this:
Please Sign up or sign in to vote.
A GroupBox automatically filled using Enums as templates.

Sample Image

Introduction

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:

[FlagsAttribute]
public enum ESpecialSort
{
 NoSpecialSort = 0,
 ElseSort = 1,
 CaseSort = ElseSort << 1
}

private ESpecialSort m_SpecialSort;

public ESpecialSort SpecialSort
{
 get
 {
 return m_SpecialSort;
 }
 set
 {
 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, _
      NSourceStructure.ProcessOptions.ESpecialSort)

    ' 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!

History

  • 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.

License

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 PinmemberMark (Code6) Belles26-Jun-06 15:21 
GeneralSame idea / different control PinmemberOrlandoCurioso22-Apr-06 5:20 
GeneralVery nice Pinmemberjpcano11-Apr-06 3:45 
GeneralRe: Very nice Pinmemberpsc_exec11-Apr-06 3:59 
GeneralRe: Very nice Pinmemberjpcano11-Apr-06 4:35 
GeneralRe: Very nice Pinmemberpsc_exec11-Apr-06 6:20 
GeneralEnum-description PinsitebuilderUwe Keim10-Apr-06 19:48 
GeneralRe: Enum-description Pinmemberpsc_exec10-Apr-06 21:23 
I think if I choose the Enum items with self-describing names the CheckBox / RadioButtons captions are meaningfull enough.
 
Peter Schlang
Software-Entwicklung und -Beratung
 
(This item consists of 100% recyclable bits and bytes!)
GeneralRe: Enum-description PinsitebuilderUwe Keim10-Apr-06 21:26 
GeneralRe: Enum-description Pinmemberpsc_exec11-Apr-06 4:04 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140709.1 | Last Updated 3 May 2006
Article Copyright 2006 by Peter Schlang
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid