Click here to Skip to main content
15,880,972 members
Articles / Programming Languages / C#
Article

A flag editor

Rate me:
Please Sign up or sign in to vote.
4.73/5 (34 votes)
17 Oct 2002 150.3K   739   65   23
This piece of code is the implementation of a simple flags editor. It can be used to edit flags in a property grid.

Sample Image - FlagsEditor.gif

Introduction

This editor displays a dropdown control holding a CheckedListBox with all the values of an enumeration.

Using the FlagsEditor

You just have to put the Editor attributes on an enumeration to link it with the FlagsEditor. A Description attribute can also be added to each value. It will be shown as a tooltip on the CheckedListBox items.

Here is a sample.

C#
[Flags,
  Editor(typeof(STUP.ComponentModel.Design.FlagsEditor), 
  typeof(System.Drawing.Design.UITypeEditor))]
public enum EnumerationTest
{
    [Description("Description for the first tested value.")]
    firstValue = 1,
    [Description("Description for the second tested value.")]
    secondValue = 2,
    [Description("Description for the third tested value.")]
    thirdValue = 4
}

How does it work ?

The Editor is just one class that inherits UITypeEditor. The behavior of Editor is controlled by two functions.

GetEditStyle

This function is used for controlling the appearance of the small button in the property grid. In this sample a dropdown arrow will be shown.

C#
public override UITypeEditorEditStyle 
       GetEditStyle(ITypeDescriptorContext context) 
{
    return UITypeEditorEditStyle.DropDown;
}

EditValue

This function is called when the user clicks on the small button.

C#
public override object EditValue(ITypeDescriptorContext context, 
        IServiceProvider provider, object value) 
{
    if (context != null
        && context.Instance != null
        && provider != null) 
    {

        // Get an instance of the IWindowsFormsEditorService. 
        edSvc = (IWindowsFormsEditorService)provider.GetService
                            (typeof(IWindowsFormsEditorService));

        if (edSvc != null) 
        {
            // Create a CheckedListBox
            clb = new CheckedListBox();

            ...

            // Show our CheckedListbox as a DropDownControl. 
            // This methods returns only when
            // the dropdowncontrol is closed
            edSvc.DropDownControl(clb);


            // return the right enum value
            // corresponding to the result
            return ...
        }
    }

    return value;
}

The DropDownControl can be closed by calling the edSvc.CloseDropDown() function.

For a complete example, just download the source file.

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
France France
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralVB.NET Port + Little Extra Feature Pin
The_Mega_ZZTer5-Aug-05 5:16
The_Mega_ZZTer5-Aug-05 5:16 
Generalcant get the zip file Pin
Paul Ashton14-Apr-05 1:10
Paul Ashton14-Apr-05 1:10 
GeneralRe: cant get the zip file Pin
The_Mega_ZZTer5-Aug-05 5:15
The_Mega_ZZTer5-Aug-05 5:15 
GeneralThank You Pin
Dave Mc Kenzie14-Oct-04 22:26
Dave Mc Kenzie14-Oct-04 22:26 
QuestionHow to use this concept for other types? Pin
DerSebastian4-Oct-04 3:29
DerSebastian4-Oct-04 3:29 
AnswerRe: How to use this concept for other types? Pin
Eric P Schneider9-Nov-05 12:15
Eric P Schneider9-Nov-05 12:15 
GeneralNice Job! Pin
glusk30-Aug-04 9:52
glusk30-Aug-04 9:52 
GeneralFlag Editor Pin
Fregate18-Dec-03 19:10
Fregate18-Dec-03 19:10 
GeneralRe: Flag Editor Pin
ShyH26-Jan-04 20:02
ShyH26-Jan-04 20:02 
GeneralIt took... Pin
James T. Johnson28-May-03 1:32
James T. Johnson28-May-03 1:32 
GeneralRe: It took... Pin
Nnamdi Onyeyiri28-May-03 3:45
Nnamdi Onyeyiri28-May-03 3:45 
GeneralIgnore the people Pin
Pete Bassett6-Dec-02 1:16
Pete Bassett6-Dec-02 1:16 
GeneralRe: Ignore the people Pin
Virender Sandhu9-Feb-03 10:28
Virender Sandhu9-Feb-03 10:28 
Questionumm ... I second the ... What is this exactly ? Pin
Anonymous18-Oct-02 17:17
Anonymous18-Oct-02 17:17 
AnswerRe: umm ... I second the ... What is this exactly ? Pin
James T. Johnson18-Oct-02 17:46
James T. Johnson18-Oct-02 17:46 
GeneralRe: umm ... I second the ... What is this exactly ? Pin
Philippe Lhoste22-Oct-02 6:32
Philippe Lhoste22-Oct-02 6:32 
GeneralRe: umm ... I second the ... What is this exactly ? Pin
stestagg10-Jul-08 2:01
stestagg10-Jul-08 2:01 
Questionumm...what is this exactly? Pin
Marc Clifton18-Oct-02 11:13
mvaMarc Clifton18-Oct-02 11:13 
AnswerRe: umm...what is this exactly? Pin
Ed.Poore24-Aug-06 5:09
Ed.Poore24-Aug-06 5:09 
GeneralRe: umm...what is this exactly? Pin
Marc Clifton24-Aug-06 8:07
mvaMarc Clifton24-Aug-06 8:07 
Ed.Poore wrote:
A very long delayed response, but yes you're being extremely dense. Especially considering your abilities now


I'm not even sure I knew what a PropertyGrid was, 4 years ago. I was coming from the MFC/C++ world, and while I still don't use designers, at least now I know what most of the UI components are. Roll eyes | :rolleyes:

Marc


Thyme In The Country

People are just notoriously impossible. --DavidCrow
There's NO excuse for not commenting your code. -- John Simmons / outlaw programmer
People who say that they will refactor their code later to make it "good" don't understand refactoring, nor the art and craft of programming. -- Josh Smith


GeneralRe: umm...what is this exactly? Pin
Ed.Poore24-Aug-06 8:19
Ed.Poore24-Aug-06 8:19 
GeneralRe: umm...what is this exactly? Pin
peterchen11-Apr-07 3:01
peterchen11-Apr-07 3:01 
General!wow Pin
NormDroid18-Oct-02 7:56
professionalNormDroid18-Oct-02 7:56 

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.