
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 RadioButton
s and CheckBox
es. The RadioButton
s/CheckBox
es 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:
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:
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
processType.SpecialSort = CType(a.Value, _
NSourceStructure.ProcessOptions.ESpecialSort)
lblSpecialSort.Text = CType(CType(a.Value, Int16), String)
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.