In Depth Banner
Skip Navigation Links

Select your preferred language

The revised UITypeEditor based class

Note how the #define works to select the appropriate code. In this example, the edit dialog is assumed to always be valid. The user has no opportunity to press cancel but you could test the return value of the dialog for standard dialog results and act accordingly if the application demands it.

#define dialog

 

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)

      {

#if dialog

        DialogTransEditor dlg=new DialogTransEditor();

        dlg.Transparency=(int)value;

        svc.ShowDialog(dlg);

        return dlg.Transparency;

#else

        TransEditControl ctrl=new TransEditControl();

        ctrl.Transparency=(int)value;

        svc.DropDownControl(ctrl);

        return (object)ctrl.Transparency;

#endif

      }

      return value;

    }

 

    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)

    {

#if dialog

      return UITypeEditorEditStyle.Modal;

#else

      return UITypeEditorEditStyle.DropDown;

#endif

    }

 

    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