Click here to Skip to main content
6,293,171 members and growing! (11,704 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, Dev
Posted:17 Oct 2002
Views:94,779
Bookmarked:51 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
39 votes for this article.
Popularity: 6.89 Rating: 4.33 out of 5
1 vote, 3.0%
1
1 vote, 3.0%
2

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


Member

Occupation: Web Developer
Location: France France

Other popular Edit Controls articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 23 of 23 (Total in Forum: 23) (Refresh)FirstPrevNext
GeneralVB.NET Port + Little Extra Feature PinmemberThe_Mega_ZZTer6:16 5 Aug '05  
Generalcant get the zip file PinsussPaul Ashton2:10 14 Apr '05  
GeneralRe: cant get the zip file PinmemberThe_Mega_ZZTer6:15 5 Aug '05  
GeneralThank You PinmemberDave Mc Kenzie23:26 14 Oct '04  
GeneralHow to use this concept for other types? PinmemberDerSebastian4:29 4 Oct '04  
GeneralRe: How to use this concept for other types? Pinmembereschneider10013:15 9 Nov '05  
GeneralNice Job! Pinmemberglusk10:52 30 Aug '04  
GeneralFlag Editor PinmemberFregate20:10 18 Dec '03  
GeneralRe: Flag Editor PinmemberShyH21:02 26 Jan '04  
GeneralIt took... PinmemberJames T. Johnson2:32 28 May '03  
GeneralRe: It took... PinmemberNnamdi Onyeyiri4:45 28 May '03  
GeneralIgnore the people PinmemberPete Bassett2:16 6 Dec '02  
GeneralRe: Ignore the people PinmemberVirender Sandhu11:28 9 Feb '03  
Generalumm ... I second the ... What is this exactly ? PinsussAnonymous18:17 18 Oct '02  
GeneralRe: umm ... I second the ... What is this exactly ? PineditorJames T. Johnson18:46 18 Oct '02  
GeneralRe: umm ... I second the ... What is this exactly ? PinmemberPhilippe Lhoste7:32 22 Oct '02  
GeneralRe: umm ... I second the ... What is this exactly ? Pinmemberstestagg3:01 10 Jul '08  
Generalumm...what is this exactly? PinmemberMarc Clifton12:13 18 Oct '02  
GeneralRe: umm...what is this exactly? PinmemberEd.Poore6:09 24 Aug '06  
GeneralRe: umm...what is this exactly? PinsupporterMarc Clifton9:07 24 Aug '06  
GeneralRe: umm...what is this exactly? PinmemberEd.Poore9:19 24 Aug '06  
GeneralRe: umm...what is this exactly? Pinsupporterpeterchen4:01 11 Apr '07  
General!wow PinmemberNorm 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-2009
Web11 | Advertise on the Code Project