Create a configurable component (paste) for Microsoft Visual Studio. NET Design Tools

Source: Internet
Author: User
Tags bool continue new set reflection sort valid visual studio
Visual for Microsoft visual Studio. NET design tools to create a design-type component


Shawn Burke
Microsoft Corporation July 2000


Summary: Microsoft. NET components are built in the language of general speech and written in management code. This article discusses how Microsoft. NET components provide developers with a new set of excellent hybrid development tools, not only like Microsoft Visual Basic, but also with lower-tier programming capabilities that are more relevant to ATL or MFC (26 pages).

Content

Simple Introduction

Examples of components

Customize to continue information

Browsing Accessibility

To be in the same way: Save components through the code

Component Design Tools

Using the design tool service and the infrastructure structure

Component Grant rights

Concludes

Simple Introduction
The Microsoft Visual studio®.net version, which Microsoft will launch, delivers an integrated environment for developers, not just the traditional C + + application, but also the new Microsoft. NET component. These new components are written using management code and built in general language, providing developers with a new set of excellent hybrid development tools, not only similar to Microsoft Visual Basic, but also available with Active Template Library (ATL) or Microsoft Foundation Class (MFC), a more relational, low-level programming capability. The control environment, which can be used as a central target, it's easy to interact with traditional COM components, and it also allows developers to spend more time building large components and reducing the amount of memory leaks, security, and the focus of a target's anxiety, too.

In addition to providing the development of Microsoft. NET Framework components, Visual Studio. NET (vs. net) has a number of accessories that allow components to take advantage of the design tool structures in vs. NET, so that the external and behavioral and the VS. NET random elements Pieces similar. In the VS. NET design tool, you will find all the capabilities of the development management components, using the. NET Framework components themselves, and allowing for the design stage and the consolidation of the components in the hierarchy.

Examples of components
Microsoft. Net Framework components are actually easy to write. If you want to perform with the Visual Studio. NET design tool, the only conditions are the System.ComponentModel.IComponent derived from the System.ComponentModel.Component, which is IComponent's preset. IComponent allows components to track design information, such as container components, names, or access to design tools for services.

For example, we write like the following simple. NET components:

Using System;
Using System.ComponentModel;
public class Booltracker:component {
private bool State;
Private EventHandler handler;
private static Object eventvaluechanged = new Object ();

Public Booltracker () {
}

public bool Value {
get {
return state;
}
set {
if (this.state!= value) {
This.state = value;
OnValueChanged (New EventArgs ());
}
}
}

public void addonvaluechanged (EventHandler h) {
Handler = (EventHandler) delegate.combine (Handler, h);
}

protected virtual void onvaluechanged (EventArgs e) {
if (handler!= null) {
Handler (this, e);
}
}

public void removeonvaluechanged (EventHandler h) {
Handler = (EventHandler) delegate.remove (Handler, h);
}

}


Obviously, this component does not function very well, but if it is placed in Visual Studio. NET Win table design tool or component design tool, you can see its name in the browser, as well as the nature of value, in which the drop-down arrow can be set to the value "true" or "hypocrisy." When the value is between "true" and "false", an event named OnValueChanged can be developed.

In a design tool, the components tend to be half the success, and the most important part is the nature of the data. There are information about categories, attributes, and events. For example, in the case of value, the material itself has been linked to the data, such as type (boolean value), line (read/write), or name (value). The basic capture uses the "reflection" to take it, "reflection" is also a generic language that allows users to check the object in their type, basic type, nature, method, build function, field, or access level for a period of time. All of this information is viewed in the data.

Customize to continue information
The dependencies in the customization include part of the custom information that can be attached to a category or category of members (a fence, a piece of material, or a method), and the type of information is usually identified by a particular client. In the Visual Studio. NET design tool, the customization continues to lay the foundation for all of these scalability. And all of the VS. NET design tools are able to understand the System.ComponentModel.MemberAttriubute, which is based on the type. This type provides the basics, so that related design tools can quickly recognize the nature of the form.

You might be able to make it easier to understand with a few examples. For example, we do not want the Value to be shown in the browser, and this can be added to the System.ComponentModel.BrowsableAttribute in order to control whether or not to browse for a certain nature.

[Browsable (False)]
public bool Value {
get {
return state;
}
set {
if (this.state!= value) {
This.state = value;
OnValueChanged (New EventArgs ());
}
}
}


In the case of a specified Attribute, if BrowsableAttribute is browsable, the C # translator automatically adds attributes. The only limiting condition is that, if there is a specified value, the construction function must be applied to the type, and the value must be constant. In this example, the BrowsableAttribute has a construction function browsable that occupies a single Boolean reference, and the translator links the information in this to the constructor, creating a case of the nature of the structure. ". If you browse for this object, you will list the attributes in the object and ignore the attributes, because the browsing nature has been labeled with the title. It looks like a piece of stuff that doesn't belong. BrowsableAttribute can also be applied to events.

The Microsoft. Net Framework contains rich components that control how the design tools and components fit together. In the following list, including a number of useful attributes, after you read deeply and understand will find more functions:

The name of the title
Describe

BrowsableAttribute
Whether control or event is shown in the browsing nature.

Bindableattribute
To judge the nature and whether the information is connected.

CategoryAttribute
Specify what kind of category to be classified in the browser ("External","configuration","line for","", etc.).

Defaulteventattribute/defaultpropertyattribute
Specifies the preset event/attributes of the object.

HelpAttribute
Specifies the description of the subject matter or event.

LicenseProviderAttribute
Indicates the licenseprovider that can provide component authorization information.

Mergablepropertyattribute
You can allow or avoid inclusions when you have selected and browse for multiple components in the browser.

Persistableattribute
The code is created by the visual design tool (such as the Win Form design tool or component design tool) to determine whether the value of the property is stored in the code.

Persistcontentsattribute
Decide whether the code generator should return to the object's non-value type and preserve the code for the property value. ICollection types are typical examples of use.

Showintoolboxattribute
Decide whether this component is allowed in the toolbox.

Toolboxitemattriubte
Specifies that the toolbox can be used to create types of ToolboxItem types



Browsing Accessibility
It is not difficult to discover that there have been many references to browsing in the previous chapters. That is because most of the components involved in the design tool are present in the browsing nature. The design tool is responsible for the organization and displays the components, but for the user modification part, it must be done in the browsing nature. In the old version, the browser shows native COM objects to display the defined subset of the data: numbers, strings, fonts, colors, images, Boolean values, and column types. In addition, other regulatory tasks are performed by the Let or conversation side. However, the VS. NET design tool allows component design tools to define their own browsing, including how to deal with any type of object that is exposed, and how users edit these types.

The. Net Framework uses these editors in a number of types, such as Dock and Anchor for System.Drawing.Color, System.Drawing.Font, or Win Forms control.

Diagram 1: A metal editor

This browsing facility offers four basic types of functionality, which are:

Numerical transformation

Sub-attributes

Column type

Pull-down/Quick Display Editor

The System.ComponentModel.TypeConverter deal with the first three functions, and the System.WinForms.Design.UITypeEditor of the editor is the last feature.

All of the types you might encounter, like the. Net Framework, have built-in TypeConverter. Let's take TypeConverter for example, explore its uses and delve into specific examples. The following code is a compact version of TypeConverter that includes some of the features of the listed method, but it must be omitted for simplicity, leaving only the core approach. All the listed methods are virtual, so they can be overridden in necessary situations. However, since TypeConverter is based on the basics rather than interface, it means that most of the important features have been built, and you can rewrite the parts of the application.

public class TypeConverter {

//
Numerical conversion method
//

To judge whether this TypeConverter
From the specified type to the target type
Public virtual bool CanConvertFrom (
ITypeDescriptorContext context,
Type sourcetype);

To judge whether this TypeConverter
From the target type to the specified type.
Public virtual bool CanConvertTo (
ITypeDescriptorContext context,
Type destinationtype);

In TypeConverter's
The value of the transformation in the target type
Public virtual Object ConvertFrom (
ITypeDescriptorContext context,
Object value,
object[] arguments);

From TypeConverters
The value of the target type is converted to the destination type.
Public virtual Object ConvertTo (
ITypeDescriptorContext context,
Object value,
Type destinationtype,
object[] arguments);

//
Example Establishment method
//

Building objects of a target type
Public virtual Object CreateInstance (
ITypeDescriptorContext context,
Persistinfo persistinfo);

Create objects of the target type and
From the established
Value of IDictionary
Public virtual Object CreateInstance (
ITypeDescriptorContext context,
IDictionary propertyvalues);

pointed out that the TypeConverter
Do you know how to build
Objects of the target type
Public virtual bool Getcreateinstancesupported (
ITypeDescriptorContext context);

To obtain the persistinfo of the number value,
Which is to say
An object that holds the state of an established number.
Public virtual Persistinfo Getpersistinfo (
ITypeDescriptorContext context,
Object value);

//
Sub-sexual way
//

Obtained for this type are displayed
Browsing in the nature of
Sub-attributes.
Public virtual PropertyDescriptorCollection GetProperties (
ITypeDescriptorContext context,
Object value,
memberattribute[] attributes);

Indicate whether this type of "+" should be displayed
In the accessibility viewer, and whether or not to display the child attributes.
Public virtual bool Getpropertiessupported (
ITypeDescriptorContext context);


//
Preset/Standard numerical methods
//

To obtain a series of preset settings of this type
"Standard" numerical value
Public Virtual StandardValuesCollection GetStandardValues (
ITypeDescriptorContext context);

Specified from
Whether the number of getstandardvalues is a complete
List of available or valid values
Public virtual bool GetStandardValuesExclusive (
ITypeDescriptorContext context);

Specifies whether the TypeConverter can be transferred back
Standard numerical Value
Public virtual bool GetStandardValuesSupported (
ITypeDescriptorContext context);

Check whether the value is a valid value for this type.
Public virtual bool IsValid (
ITypeDescriptorContext context, object value);

//
Common Program Mode
//

A sort of a collection of attributes that
Name array to sort.
Protected PropertyDescriptorCollection sortproperties (
PropertyDescriptorCollection Props,
String[] names);
}

Public interface Itypedescriptorcontext:iserviceobjectprovider {

IContainer with this text
IContainer Container {get;}

Pass back the example of the experiment.
Whether the numbers have been sent to TypeConverter,
ITypeDescriptorContext:: The actual example will be returned
An object that provides a value.
Object Instance {get;}

From the actual example, the object is called before it is changed.
If the "hypocrisy" is returned, it cannot be changed.
and must be immediately interrupted.
BOOL Oncomponentchanging ();

From the fact that the object was returned
Make the call after the change.
void OnComponentChanged ();
}


It is not hard to see that TypeConverter Aleppo. It also contains a simple description of the ITypeDescriptorContext, because it appears in the TypeConverter method of most numbers in the form of parameters. ITypeDescriptorContext can allow access to the use of services, as well as to provide arbitrary values and transmit to the TypeConverter component objects.

However, if you look closely at the TypeConverter, you will find that it is not as complicated as you think. Its functions can be divided into four groups:

Numerical conversion: such as browsing the client side, must be easy to the string representation of the transformation type. TypeConverter can provide a set of standard methodologies that provide a predetermined data method to determine whether a given transition is possible. Since string conversion is common, TypeConverter provides Helper methods for convertfromstring and converttostring (not shown).

Example establishment and preservation (persistence): TypeConverter is responsible for the established type of case. TypeConverter does not force the client to check the build function or get the preset value, instead, it allows the type of author to control the entire process. In this way, TypeConverter also acts as a component to save roles in the design tool. It can also create and consume Persistinfo objects, which describe the state part of the object in which the preservation is related (usually including the code to come), and how to save it by hand.

Sub-method: TypeConverter allows objects to control the apparent child nature of the browser. System.Drawing.Font is an example of what is shown when the actual exhibit does not fully conform to the physical attributes of the Font object itself. To achieve clarity and usability and change and restructure.

Standard values: such as enums and many types, all contain a defined subset of numbers. Other types may also have a predetermined set, but may accept other values at the same time. TypeConverter allows clients to check out these bills and check to see if the values are acceptable.

In two ways, TypeConverter can choose one of these methods to be related to the property. A type can specify its TypeConverter by TypeConverterAttribute in the form of a Declaration (class declaration). These types typically use a nested converter to combine type and design information into one unit of a single bit of time. In addition, the TypeConverter can be specified in the nature declaration itself to overwrite the typeconverter of the type. Of course, the specified TypeConverter must have knowledge of that particular type. You may not specify any typeconverter individually, but you can use this method if you want to improve the display of the established attributes, add or remove the child, or add additional support for different strings. You can obtain and complete the improvement work by a type of preset typeconverter, and designate the type of the acquired as a typeconverter of the established nature. The other part of the browser is related to the Nature editor. The structure of the fabric editor is not as complex as typeconverters. To maximize the performance of editors who are not dependent or not connected to the Win form, the editor has no basic Editor type of editor. Since the editor is typically driven by the user interface (UI), all standard editors in the. Net Framework come from System.Drawing.Design.UITypeEditor.

public class UITypeEditor {

Number to call after editing.
Public virtual Object EditValue (
ITypeDescriptorContext context, Iserviceobjectprovider provider,
Object value);

Specifies whether this editor can be traced
Numerical value.
Public virtual bool GetPaintValueSupported (
ITypeDescriptorContext context);

If so, return the UI of this editor.
Public virtual Uitypeeditoreditstyle GetEditStyle (
ITypeDescriptorContext context);

When the client wants to describe the UI representation of the value
Go to the phone.
public virtual void PaintValue (
ITypeDescriptorContext context,
Object value,
Graphics Canvas,
Rectangle Rectangle);
}

public enum Uitypeeditoreditstyle {

No Interactive UI components
None = 1,

Compelling response UI. The nature shows 
button to activate the conversation cube.
Modal = 2,

The Drop-down UI. The nature shows
Down arrow button, and UI
In a drop-down list of similar grouped dropdown pieces.
DropDown = 3
}


This interface can be called by calling the Uitypeeditor::editvalue in the
Provider. Getserviceobject (typeof (Iwinformseditorservice))
Made.
Public interface Iwinformseditorservice {

Closes the Drop-down list currently displayed
void Closedropdown ();

The UI editor of the master number value
An established control in a Drop-down list.
void Dropdowncontrol (Control control);

To activate the system to respond to the conversation square, to edit the value of the attributes.
DialogResult ShowDialog (Form Dialog);
}


EditorAttribute and TypeConverterAttribute can be specified in the form of a type or specific type, and can be overridden by the values specified by the type itself.

Users will call Uitypeeditor.editvalue when they click the Pull-down or elliptical orbits button in the browser. The following is a typical example of a EditValue in a drop-down editor:

public override Object EditValue (
ITypeDescriptorContext context, Iserviceobjectprovider provider,
Object value) {
Object returnvalue = value;

if (provider!= null) {
Iwinformseditorservice edsvc = (iwinformseditorservice)
Provider. Getserviceobject (
typeof (Iwinformseditorservice));

if (edsvc!= null) {
Myuieditorcontrol Uieditor =
New Myuieditorcontrol (EDSVC);
Edsvc.dropdowncontrol (Uieditor);
value = Uieditor.newvalue;
}
}

return value;
}


PaintValue allows the editor to display a visual representation of a particular value. For example, WinForms objects can use this editor to edit images, colors, and fonts.

Figure 2: Editor display screen

Examples of code based:

public override void PaintValue (
ITypeDescriptorContext context,
Object value,
Graphics Canvas,
Rectangle Rectangle) {
If (value is Color) {
Color color = (color) value;
SolidBrush B = new SolidBrush (color);
Canvas. FillRectangle (b, Rectangle);
B.dispose ();
}
}

Coloreditor ' s PaintValue code.





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.