To write a RAD component that interacts with the. NET Properties Window (v)

Source: Internet
Author: User
Tags visibility visual studio
Interactive Eight, providing an interactive view of properties
When you are in Visual C #. NET to create a project, you may notice that there is a button on the toolbar of the property window that looks like a lightning bolt, press the Button Property window to switch to the event view, so you can edit the event processing.

The view of the Properties window is from property Tabs, so the main class used by the view is PropertyTab, and the namespace is System.Windows.Forms.Design. A property page can be associated with a particular component, design document, or a static association that can be used. The property pages associated with a component or document are specified with the Propertytabattribute attribute on the class. This attribute specifies the type of tab to create, whether it is indicated by the Propertytabattribute propertytabscope parameter on the property window. The visibility of a property page specified as a component range is determined by the visibility of the component with the Propertytabattribute attribute. Document-scoped Property pages can be visible in the design of the current project. His default value is Propertytabscope.component.

For example, take a look at the "Funkybutton" project. Funkybutton is a UserControl that expands the PropertyTab, and allows us to make the control an irregular polygon.




Figure 6. Funkybutton





The currently selected property page is the property window that is obtained from the properties of the selected control. Property pages allow you to manipulate different collections of display properties. The events page handles the event in some way, just like a property. In this example, the property page creates a property that represents the vertex of the control.

The properties in the. NET framework are encapsulated with the PropertyDescriptor class. The PropertyDescriptor itself is an abstract class, in which the classes derived from the framework provide a way to access the component's open properties. However, the property window is directly acting on the propertydescriptor rather than directly on the property. Therefore, we can write our own propertydescriptor to do some special work. In this example, we have an attribute that represents the number of vertices of the control, and the other represents each vertex. Again, the addition of pages on the Properties window does not work on other objects.

When the property window asks the PropertyTab for properties, it calls the GetProperties method. For our sample program, this method looks like the following:

public override PropertyDescriptorCollection

GetProperties (ITypeDescriptorContext context, object component,

Attribute[] attrs)

{

Our list of props.

//

ArrayList proplist = new ArrayList ();





Add the property to our count of vertices

//

Proplist.add (This) (new Numpointspropertydescriptor);





Add a property descriptor to each vertex

//

for (inti = 0; I < ((Funkybutton) component). Points.count; i++)

{

Proplist.add (New Vertexpropertydescriptor (this,i));

}





Return the collection of PropertyDescriptors.

propertydescriptor[] Props =

(propertydescriptor[]) Proplist.toarray (typeof (PropertyDescriptor));

return new PropertyDescriptorCollection (props);

}

GetProperties simply returns a collection of attribute descriptions. PropertyDescriptors is quite simple, look at the code carefully to see how they work.

Funkybutton also examples of the implementation of the Drop-down List Editor. For each point, we are not simply the X and y values of the input coordinates, we will figure out the shape of the Funkybutton, and we can use the graphical method to change the position of the point. The editing style of this setting is more friendly.




Figure 7. A graphical point vector

Because the custom PropertyTab provides properties, overloading the editor of this property is also easy. Simply overload the PropertyDescriptor Geteditor method, and then return to the instance of the custom component.

public override Object Geteditor (Type editorbasetype)

{

Make sure we ' re looking for a uitypeeditor.

//

if (Editorbasetype = = typeof (System.Drawing.Design.UITypeEditor))

{

Create and return one of our editors.

//

if (editor = null)

{

Editor = new Pointuieditor (owner.target);

}

return editor;

}

Return base. Geteditor (Editorbasetype);

}

The design editor is just as simple. The editor is a simple UserControl, so we can do it like designing other WindowsForms objects.




Figure 8. Designing the Editor





Finally, when the user clicks the Drop-down arrow in the Properties window, our editor can flick the UI editor just now. The Uitypeeditor.editvalue overload in the Pointuieditor can be implemented.

public override Object EditValue (

ITypeDescriptorContext context,

IServiceProvider SP, Object value)

{

Get the editor service.

IWindowsFormsEditorService edsvc =

(IWindowsFormsEditorService) Sp. GetService (typeof (IWindowsFormsEditorService));





Create our UI

if (UI = null)

{

UI = new Pointeditorcontrol ();

}



Initialize the UI with the settings for this vertex

Ui. Selectedpoint = (point) value;

Ui. Editorservice = edsvc;

Ui. Target = (Funkybutton) context. Instance;



Instruct the editor service to display the control as a

Dropdown.

Edsvc.dropdowncontrol (UI);



return the updated value;

Return UI. Selectedpoint;

}

Nine, we can also use it
You can have the same characteristics as the IDE Properties window in your own application. Add the System.Windows.Forms.PropertyGrid control to the toolbox in the IDE by capturing PropertyGrid in the Toolbox component tag.

PropertyGrid and other controls work the same. You can anchor or dock him, change its color and so on. The following list lists some of the interesting properties of PropertyGrid.

Selectedobject
PropertyGrid the object to display

toolbarvisible
Show or hide the toolbar at the top of the PropertyGrid

helpvisible
Show or hide help text at the bottom of the PropertyGrid

PropertySort
Sets the sort type of the PropertyGrid (categorized, alphabetical, etc.).


These properties can be set at design time. At run time, you can manipulate PropertyGrid to let him show you the object. An example of a button is shown below. In this case, Porpertygrid's help and Toolbox are hidden. As mentioned above, you can set his own properties.




Figure 9. Hides the PropertyGrid of toolbar and help information

X. CONCLUSION
NET framework and visual Studio. NET adds a considerable amount of functionality to the Property window. Because the Properties window is the core of RAD, these features can be extended at the same time as ease of use, and are therefore common in Visual Basic. Just as we can use PropertyGrid in our programs, we can put more time on how to write a program to simplify our UI work.


-----------------------------------------------------------------------------------------

<<<<<<<<<<<<<<<<<<< to Be Continued >>>>>>>> >>>>>>>>>>>>>>



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.