Developing Custom .NET Component Designers

Download Report

Transcript Developing Custom .NET Component Designers

Developing Custom .NET
Component Designers
DevCon 2005 -- Course No: 4106
Ray Konopka
Agenda
Component Techniques
Type Converters
UI Type Editors
Designers
2
DesignMode
DesignMode is true if the component is currently be
used at design-time
NOTE
 DesignMode should not be checked from within the
constructor or any code called from the constructor.
 Constructor called before component assigned to a
site.
 Site determines whether component is in design
mode or not.
3
Component Related Attributes
Category
 Specify category property will be grouped in Object
Inspector
Description
 Specify a short description displayed when associated
property is selected in Object Inspector
Browsable
 Determines whether the associated property is visible
in Object Inspector
 public vs. published.
5
Component Related Attributes
ReadOnly
 Determines if associated property can be edited in
Object Inspector.
DefaultValue
 Specify a value to be considered “default” for the
property
Localizable
 Property value can be localized without modifying the
source code
6
Component Related Attributes
MergableProperty
 Determines if associated property is visible when
multiple components are selected
RefreshProperties
 Associate with a property that changes other property
values
DefaultEvent
 Specify event the form designer will hook up when
the control is double-clicked.
7
Component Related Attributes
ToolboxBitmap
 Associate an image with the component for display in
a toolbox
 16x16 pixel 16-color image or icon added to assembly
as a resource
8
Component Palette Images
Typically avoid using ToolboxBitmap
Instead, just add bitmap or icon file to your
assembly project
(In Delphi) Filename must match name of
component class and include the namespace
 Raize.Samples.WinForms.Delphi.RkSpinner.bmp
9
Type Converters
System.ComponentModel.TypeConverter
Performs similar role to VCL Property Editors
Convert one type to another and back
 Integer <-> String
 Enum <-> String
Property Inspector uses a type converter for each
property displayed
Can be used outside of design environment
Examples
 StringConverter, Int32Converter, DateTimeConverter
10
Custom TypeConverters
TypeConverters can also handle complex properties
 e.g. sub-properties
Hand = class
private
FLineColor: Color;
FWidth: Integer;
public
constructor Create( aLineColor: Color; aWidth: Integer );
published
property LineColor: Color
read FLineColor
write FLineColor;
property Width: Integer
read FWidth
write FWidth;
end;
11
Create Custom TypeConverter
Descend from TypeConverter or descendant
Override appropriate methods
 CanConvertFrom
 ConvertFrom
 ConvertTo
Associate converter with property using
TypeConverter attribute
12
Example
RkClock
 HourHand, MinuteHand, SecondHand properties
No Converter
Basic Text Converter
ExpandableObjectConverter
13
Serialization with TypeConverters
Hand properties are displayable in Property
Inspector
But even if you make changes, they are not
serialized
Need to do some extra steps
 Create a private ShouldSerialXxxx method
 Create a private ResetXxxx method
 Return an InstanceDescriptor in ConvertTo method
14
Example
Enhanced HandTypeConverter.ConvertTo method
15
UITypeEditors
Provides enhanced editing capabilities in Property
Inspector
Also similar to VCL Property Editors
System.Drawing.Design.UITypeEditor
Examples
 FontEditor
 ColorEditor
 ImageEditor
16
Custom UITypeEditors
Create a descendant class
Override GetEditStyle method
 UITypeEditorEditStyle.DropDown
 UITypeEditorEditStyle.Modal
 UITypeEditorEditStyle.None
Override EditValue
Optionally override additional methods
 GetPaintValueSupported
 PaintValue
Associate with property with Editor attribute
17
Example
RkLabel with StringEditor
18
Designers
Similar in function to VCL Component Editors
System.ComponentModel.Design.ComponentDesigner
 System.Windows.Forms.Design.ControlDesigner
 System.Web.UI.Design.ControlDesigner
Handles Design-Time Display changes
Context Menu Items
Design-Time only properties
19
Custom Designers
Descend from appropriate base class
Override Various methods
 OnPaintAdornments
 PreFilterProperties
 DesignerVerbs
20
Example
RkClockDesigner
 OnPaintAdornments
 Design-Time Only Property
21
The Finish Line
Contact Information
Ray Konopka
[email protected]
http://www.raize.com
Evaluation Forms
Questions & Answers
22