Click here to Skip to main content
15,867,330 members
Articles / Programming Languages / Visual Basic
Article

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 63.9K   822   36   15
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:

C#
[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:

VB
'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:
    VB
    ' 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:

VB
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


Written By
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 15:21
Mark Belles26-Jun-06 15:21 
GeneralSame idea / different control Pin
OrlandoCurioso22-Apr-06 5:20
OrlandoCurioso22-Apr-06 5:20 
GeneralVery nice Pin
jpcano11-Apr-06 3:45
jpcano11-Apr-06 3:45 
GeneralRe: Very nice Pin
Peter Schlang11-Apr-06 3:59
Peter Schlang11-Apr-06 3:59 
GeneralRe: Very nice Pin
jpcano11-Apr-06 4:35
jpcano11-Apr-06 4:35 
GeneralRe: Very nice Pin
Peter Schlang11-Apr-06 6:20
Peter Schlang11-Apr-06 6:20 
No, the Enum template MUST be a property in an external class.
It is not necessary that the class is named ProcessOptions, and you may have a different class for every Enum.
But it is not possible to define a variable of type Enum in your dialog as a template for the EnumGroupBox.

Peter Schlang
Software-Entwicklung und -Beratung

(This item consists of 100% recyclable bits and bytes!)
GeneralEnum-description Pin
Uwe Keim10-Apr-06 19:48
sitebuilderUwe Keim10-Apr-06 19:48 
GeneralRe: Enum-description Pin
Peter Schlang10-Apr-06 21:23
Peter Schlang10-Apr-06 21:23 
GeneralRe: Enum-description Pin
Uwe Keim10-Apr-06 21:26
sitebuilderUwe Keim10-Apr-06 21:26 
GeneralRe: Enum-description Pin
Peter Schlang11-Apr-06 4:04
Peter Schlang11-Apr-06 4:04 
GeneralRe: Enum-description Pin
Uwe Keim11-Apr-06 18:48
sitebuilderUwe Keim11-Apr-06 18:48 
GeneralRe: Enum-description Pin
Peter Schlang15-Apr-06 1:05
Peter Schlang15-Apr-06 1:05 
GeneralRe: Enum-description Pin
Uwe Keim15-Apr-06 1:33
sitebuilderUwe Keim15-Apr-06 1:33 
GeneralNice! Pin
Marc Clifton10-Apr-06 11:57
mvaMarc Clifton10-Apr-06 11:57 
GeneralRe: Nice! Pin
Peter Schlang11-Apr-06 4:12
Peter Schlang11-Apr-06 4: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.