Click here to Skip to main content
Licence 
First Posted 17 Oct 2002
Views 114,593
Bookmarked 58 times

A flag editor

By | 17 Oct 2002 | Article
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.

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

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

EditValue

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

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

About the Author

Thierry Bouquain

Web Developer

France France

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralVB.NET Port + Little Extra Feature PinmemberThe_Mega_ZZTer5:16 5 Aug '05  
Generalcant get the zip file PinsussPaul Ashton1:10 14 Apr '05  
GeneralRe: cant get the zip file PinmemberThe_Mega_ZZTer5:15 5 Aug '05  
GeneralThank You PinmemberDave Mc Kenzie22:26 14 Oct '04  
QuestionHow to use this concept for other types? PinmemberDerSebastian3:29 4 Oct '04  
AnswerRe: How to use this concept for other types? Pinmembereschneider10012:15 9 Nov '05  
GeneralNice Job! Pinmemberglusk9:52 30 Aug '04  
GeneralFlag Editor PinmemberFregate19:10 18 Dec '03  
GeneralRe: Flag Editor PinmemberShyH20:02 26 Jan '04  
GeneralIt took... PinmemberJames T. Johnson1:32 28 May '03  
GeneralRe: It took... PinmemberNnamdi Onyeyiri3:45 28 May '03  
GeneralIgnore the people PinmemberPete Bassett1:16 6 Dec '02  
GeneralRe: Ignore the people PinmemberVirender Sandhu10:28 9 Feb '03  
Questionumm ... I second the ... What is this exactly ? PinsussAnonymous17:17 18 Oct '02  
AnswerRe: umm ... I second the ... What is this exactly ? PineditorJames T. Johnson17:46 18 Oct '02  
GeneralRe: umm ... I second the ... What is this exactly ? PinmemberPhilippe Lhoste6:32 22 Oct '02  
GeneralRe: umm ... I second the ... What is this exactly ? Pinmemberstestagg2:01 10 Jul '08  
Questionumm...what is this exactly? PinmemberMarc Clifton11:13 18 Oct '02  
AnswerRe: umm...what is this exactly? PinmemberEd.Poore5:09 24 Aug '06  
GeneralRe: umm...what is this exactly? PinprotectorMarc Clifton8:07 24 Aug '06  
GeneralRe: umm...what is this exactly? PinmemberEd.Poore8:19 24 Aug '06  
Well you're at the stage where you're
 
writeDesigner:
    this.WriteDesignerForDesigner();
    goto writeDesigner;

 
As of how to accomplish this, have you ever tried Google?
GeneralRe: umm...what is this exactly? Pinmemberpeterchen3:01 11 Apr '07  
General!wow PinmemberNorm Almond7:56 18 Oct '02  

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.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120529.1 | Last Updated 18 Oct 2002
Article Copyright 2002 by Thierry Bouquain
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid