In Depth Banner
Skip Navigation Links

Select your preferred language

A class derived from UITypeEditor.

The EditValue method creates an instance of the dropdown editor control and uses it to edit the integer value.

The GetEditStyle is called by the property grid to ascertain what type of editor this is. It returns the Dropdown style because this is indeed a dropdown editor.

The GetPaintValueSupported method override returns true because this class can indeed provide a custom representation of the value.

The PaintValue method shows a visual representation of transparency by displaying a red rectangle which overprints a black rectangle depending upon the transparency setting. The more transparent it is, the more the black shows through the red.

 

using System;

using System.ComponentModel;

using System.Drawing;

using System.Drawing.Design;

using System.Windows.Forms.Design;

 

namespace TypeEditorsCS

{

  /// <summary>

  /// Summary description for TransparencyEditor.

  /// </summary>

  public class TransparencyEditor : UITypeEditor

  {

    public TransparencyEditor()

    {

    }

 

    public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)

    {

      IWindowsFormsEditorService svc=(IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));

      if(svc!=null)

      {

        TransEditControl ctrl=new TransEditControl();

        ctrl.Transparency=(int)value;

        svc.DropDownControl(ctrl);

        return (object)ctrl.Transparency;

      }

      return value;

    }

 

    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)

    {

      return UITypeEditorEditStyle.DropDown;

    }

 

    public override bool GetPaintValueSupported(ITypeDescriptorContext context)

    {

      return true;

    }

 

    public override void PaintValue(PaintValueEventArgs e)

    {

      Rectangle rc=e.Bounds;

      rc.Inflate(-3,-3);

      e.Graphics.FillRectangle(Brushes.Black,rc);

      rc=e.Bounds;

      rc.Offset(e.Bounds.Width/2,0);

      rc.Width=rc.Width/2;

      SolidBrush sb=new SolidBrush(Color.FromArgb((int)e.Value,Color.Red));

      e.Graphics.FillRectangle(sb,rc);

      sb.Dispose();

    }

  }

}

 

Return to the article.

Copyright © Bob Powell 2003-2009. All rights reserved