|
|
|
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();
}
}
}
#Const
dialog = True
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
#If
dialog Then
Dim
dlg As DialogTransEditor =
New DialogTransEditor
dlg.Transparency = CType(value,
Integer)
svc.ShowDialog(dlg)
Return
dlg.Transparency
#Else
Dim
ctrl As TransEditControl =
New TransEditControl
ctrl.Transparency
= CType(value, Integer)
svc.DropDownControl(ctrl)
Return
CType(ctrl.Transparency,
Object)
#End
If
End
If
Return
value
End Function
Public Overloads
Overrides Function
GetEditStyle(ByVal context
As ITypeDescriptorContext)
As UITypeEditorEditStyle
#If
dialog Then
Return
UITypeEditorEditStyle.Modal
#Else
Return
UITypeEditorEditStyle.DropDown
#End
If
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.