Writing and. NET Properties Window Interactive Rad component (ii)

Source: Internet
Author: User
Tags bool resource
Interaction v. Basic knowledge: Using attribute to customize the display of a property window
The mechanism for controlling the display is the same as the component defined with IDL, but adds the metadata attribute. The most commonly used feature of control display is BrowsableAttribute. By default, the Properties window displays all the exposed, readable (that is, public, have get, or set methods) properties defined in the object and places them in the Miscellaneous (MISC) category. The following is a simple example of a component:

public class SimpleComponent:System.ComponentModel.Component

{

private String data = "(none)";

private bool Datavalid = false;








public string Data

{

Get

{

return data;

}

Set

{

if (value!= data)

{

Datavalid = true;

data = value;

}

}

}

public bool Isdatavalid

{

Get

{

Perform some check on the data

//

return datavalid;

}

}

}


The following illustration shows this example in the Properties window:












Figure 1. A simple component that appears in the Properties window

In this example, the SimpleComponent has two properties: Data and Isdatavalid. In fact, because Isdatavalid is read-only, it doesn't make much sense to display here, and it's not necessary for designers to know the value of this property in the design state. So we give him the BrowsableAttribute feature so that the property window doesn't show him.

[Browsable (False)]

public bool Isdatavalid

{

Get

{

Perform some check on the data

//

return datavalid;

}

}


The compiler automatically adds "attribute" characters after the attribute class name, so we can omit him from the code. Of course, entering "[BrowsableAttribute (false)]" is the same effect. For properties or classes that do not specify attributes, the compiler uses default attributes and default attribute values to describe them. In this example, the default value for BrowsableAttribute is true. This principle is for visual Basic. NET is also consistent. The only difference between the two is visual Basic. NET uses angle brackets (' < ' and ' > ') to mark attributes instead of brackets (' [' and '] ') used in C #.


The compiler automatically adds "attribute" characters after the attribute class name, so we can omit him from the code. Of course, entering "[BrowsableAttribute (false)]" is the same effect. For properties or classes that do not specify attributes, the compiler uses default attributes and default attribute values to describe them. In this example, the default value for BrowsableAttribute is true. This principle is for visual Basic. NET is also consistent. The only difference between the two is visual Basic. NET uses angle brackets (' < ' and ' > ') to mark attributes instead of brackets (' [' and '] ') used in C #.

Also, we note that the value "ABC" of the Data property in Figure 1 is bold. This means that the value of the attribute is not the default value, and that the value is saved when the designer generates code for form or control (that is, an assignment statement is generated). The default value for a property is not necessary to generate an assignment statement, and generating code means increasing the time that the component is initialized (the InitializeComponent method) and the size of the code file. So how do you simplecomponent to notify the property window of the default value? To implement this feature, we need to describe the attribute using the DefaultValueAttribute attribute, and we can assign a value to the property in the object's builder. When a property window displays a property value, it compares the current value with the default value specified by DefaultValueAttribute, and if the two are not equal, the value is displayed in bold. In the following example, the value of the Data property is displayed in bold if it is not "(none)".

[DefaultValue (none)]

public string Data

{

// . . .

}

We can also add more complex judgment logic to the attribute rather than just some simple intrinsic value comparisons, which can be achieved by adding some special methods to the component. The name of the logical method must begin with "ShouldSerialize" and then the name of the attribute, and the return value of this method is "Boolean". In this case, this method is called "Shouldserializedata". Adding the following code to the SimpleComponent component can achieve the same effect as DefaultValueAttribute, but he can have stronger logical code.

private bool Shouldserializedata ()

{

return Data!= "(none)";

}

In general, attribute classification is more user-friendly for designers. We just use the CategoryAttribute attribute to classify attributes. This feature uses a simple class-object string that the property window can use to display the attribute in the class-purpose subkey. The name of the class head can be determined by itself.

[DefaultValue ("(none)"), Category ("Sample")]

public string Data

{

// . . .

}

One problem that component developers often encounter is how to localize this kind of mesh string. Let's take a look at the CategoryAttribute class and see how his Getlocalizedstring method provides such functionality. To localize a class-like string, you derive a new attribute class from the CategoryAttribute class. In this example, we get a localized string of literals with key values indexed from the string resources of the component. When specifying the CategoryAttribute attribute of a property, use this key value (key) to replace the original class name as an input parameter. This allows the property window to call the Getlocalizedstring method in the CategoryAttribute of the query property and to pass the key value as a parameter to the method, displaying the class name of the returned property in the Properties window.

Internal class Loccategoryattribute:categoryattribute

{

Public Loccategoryattribute (String categorykey): Base (Categorykey)

{

}








protected override string Getlocalizedstring (string key)

{

Get the resource set for the current locale.

//

ResourceManager ResourceManager = new ResourceManager ();

string CategoryName = null;








Walk up the cultures until we find one with

This key as a resource as our category name

If we reach the invariant culture, quit.

//

for (CultureInfo culture = cultureinfo.currentculture;

CategoryName = = NULL &&

Culture!= CultureInfo.InvariantCulture;

Culture = culture. Parent)

{

CategoryName = (string) resourcemanager.getobject (key, culture);

}

return CategoryName;

}

}

To use this localized class name, we first define a resource file that contains the key, and use the attribute to go to the attribute.

[Loccategory ("Samplekey")]

public string Data

{

// . . .

}

When we design the window, if you select multiple components on the form, the Properties window displays their common properties, which are distinguished by the component's property name and type. If you change the value of a common attribute, then all of the selected components will have the same property value changed accordingly. It is generally not meaningful to include an attribute in the synthesis of other attributes. Typically, common attributes have unique values, such as the name of another component. Because when multiple components are selected, changing the value of the common attribute, the corresponding property values of all selected components will change, so you need to specify that some properties are not merged into common attributes. Mergablepropertyattribute can do this. If you simply set this parameter to False, you can hide the attribute when selecting multiple components, and naturally his value will not change.

The values of some properties can affect some other property values. For example, in a component that implements data binding, it is clear that the value of the DataSource should be cleared away from the values of the DataMember. Refreshpropertiesattribute let us achieve this function. His default argument is "None," which does not affect other attributes, and if you specify additional attribute parameters, the property window can automatically update the values of other properties while changing the value of this property. The other two values, one of which is repaint, lets the property window regain the value of the property and redraw them; and all, which means that the component itself will regain the value of the property. If the value change causes the number of attributes to increase or decrease, then we will use all. Note, however, that this is generally used in high-level situations and is slower than repaint. Refreshproperties.repaint is applicable to most situations. It should be noted that this feature is used on attributes that trigger changes, not on attributes that are adapted.

Finally, Defaultpropertyattribute and Defaulteventattribute use a class-level situation where they let the Properties window highlight these properties and events. When you select a different component, the Properties window always attempts to navigate to the same properties or events as the last selected component. If no such attribute or event is found, it navigates the input pointer to the property specified by Defaultpropertyattribute. The event view navigates to the events specified by Defaulteventattribute. Also, this event is an automatically used event when you double-click the component.

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

<<<<<<<<<<<< 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.