5,696,576 members and growing! (16,460 online)
Email Password   helpLost your password?
Desktop Development » Edit Controls » General     Intermediate

A flag editor

By Thierry Bouquain

This piece of code is the implementation of a simple flags editor. It can be used to edit flags in a property grid.
C#, Windows, .NET 1.0, .NETVisual Studio, VS.NET2002, Dev

Posted: 17 Oct 2002
Updated: 17 Oct 2002
Views: 88,799
Bookmarked: 48 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
39 votes for this Article.
Popularity: 6.89 Rating: 4.33 out of 5
1 vote, 3.0%
1
1 vote, 3.0%
2
0 votes, 0.0%
3
8 votes, 24.2%
4
23 votes, 69.7%
5

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



Occupation: Web Developer
Location: France France

Other popular Edit Controls articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 23 of 23 (Total in Forum: 23) (Refresh)FirstPrevNext
GeneralVB.NET Port + Little Extra FeaturememberThe_Mega_ZZTer6:16 5 Aug '05  
Generalcant get the zip filesussPaul Ashton2:10 14 Apr '05  
GeneralRe: cant get the zip filememberThe_Mega_ZZTer6:15 5 Aug '05  
GeneralThank YoumemberDave Mc Kenzie23:26 14 Oct '04  
GeneralHow to use this concept for other types?memberDerSebastian4:29 4 Oct '04  
GeneralRe: How to use this concept for other types?membereschneider10013:15 9 Nov '05  
GeneralNice Job!memberglusk10:52 30 Aug '04  
GeneralFlag EditormemberFregate20:10 18 Dec '03  
GeneralRe: Flag EditormemberShyH21:02 26 Jan '04  
GeneralIt took...memberJames T. Johnson2:32 28 May '03  
GeneralRe: It took...memberNnamdi Onyeyiri4:45 28 May '03  
GeneralIgnore the peoplememberPete Bassett2:16 6 Dec '02  
GeneralRe: Ignore the peoplememberVirender Sandhu11:28 9 Feb '03  
Generalumm ... I second the ... What is this exactly ?sussAnonymous18:17 18 Oct '02  
GeneralRe: umm ... I second the ... What is this exactly ?editorJames T. Johnson18:46 18 Oct '02  
GeneralRe: umm ... I second the ... What is this exactly ?memberPhilippe Lhoste7:32 22 Oct '02  
GeneralRe: umm ... I second the ... What is this exactly ?memberstestagg3:01 10 Jul '08  
Generalumm...what is this exactly?memberMarc Clifton12:13 18 Oct '02  
GeneralRe: umm...what is this exactly?memberEd.Poore6:09 24 Aug '06  
GeneralRe: umm...what is this exactly?supporterMarc Clifton9:07 24 Aug '06  
GeneralRe: umm...what is this exactly?memberEd.Poore9:19 24 Aug '06  
GeneralRe: umm...what is this exactly?supporterpeterchen4:01 11 Apr '07  
General!wowmemberNorm Almond8:56 18 Oct '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 17 Oct 2002
Editor: Smitha Vijayan
Copyright 2002 by Thierry Bouquain
Everything else Copyright © CodeProject, 1999-2008
Web13 | Advertise on the Code Project