|
|
|
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();
}
}
}
Imports
System
Imports
System.ComponentModel
Imports
System.Drawing
Imports
System.Drawing.Design
Imports
System.Windows.Forms.Design
Public
Class TransparencyEditor
Inherits UITypeEditor
Public Overloads
Overrides Function
EditValue(ByVal context
As ITypeDescriptorContext, ByVal provider
As IServiceProvider,
ByVal value As
Object) As
Object
Dim
svc As IWindowsFormsEditorService =
CType(provider.GetService(GetType(IWindowsFormsEditorService)),
IWindowsFormsEditorService)
If
Not svc Is
Nothing Then
Dim
ctrl As TransEditControl =
New TransEditControl
ctrl.Transparency
= CType(value, Integer)
svc.DropDownControl(ctrl)
Return
CType(ctrl.Transparency,
Object)
End
If
Return
value
End Function
Public Overloads
Overrides Function
GetEditStyle(ByVal context
As ITypeDescriptorContext)
As UITypeEditorEditStyle
Return
UITypeEditorEditStyle.DropDown
End Function
Public Overloads
Overrides Function
GetPaintValueSupported(ByVal context
As ITypeDescriptorContext)
As Boolean
Return
True
End Function
Public Overloads
Overrides Sub
PaintValue(ByVal e As
PaintValueEventArgs)
Dim
rc As Rectangle = 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
Dim
sb As SolidBrush = New
SolidBrush(Color.FromArgb(CInt(e.Value),
Color.Red))
e.Graphics.FillRectangle(sb,
rc)
sb.Dispose()
End Sub
End
Class
Return to the article.