The previous article introduced the commonly used design time attribute. of which Browsableattribute,categoryattribute, Descriptionattribute,defaultpropertyattribute, Defaulteventattribute are simpler and dispensable, but in order to provide a better user experience these attributes are best not to be omitted, if you are not familiar with these attributes, you can refer to my previous Article description or view MSDN, I'm not going to repeat it here.
Down we mainly introduce DesignerSerializationVisibilityAttribute and TypeConverterAttribute.
DesignerSerializationVisibilityAttribute's function is to indicate whether a property is serialized and serialized, its value is an enumeration, and there are three types of content,hidden,visible. Content instructs the code generator to generate code for the content that the object contains, not for the object itself, hidden instructs the code generator not to generate code for the object, visible instructs the code generator to generate code for the object. If your control has a collection property and wants to automatically generate code for the contents of the collection properties at design time, use this attribute and set the value to Designerserializationvisibility.content.
The role of TypeConverterAttribute is much larger and slightly more complex. TypeConverterAttribute The main purpose is to specify a type converter for the attribute, which converts the value of the property to other types of the city. NET Framework has provided a type converter for most commonly used types, such as color Colorconverter, enumeration type Enumconverter, and so on, so you don't normally have to write type converters. If you have a special type of attribute or a custom type then you have to write it. Type converters are all derived from System.ComponentModel.TypeConverter, and you need to rewrite some of these methods to achieve the purpose of the conversion, and in the course of our development, we only care about how the value of the attribute is converted to a string (because the value of the attribute requires In the browser, the property browser displays a string of characters and the source code (you need to automatically generate the source code for the property's value to implement persistence), and, in turn, convert the string and source code to the value of the property. Alternatively, you can implement child properties by using TypeConverter, so that the properties ' child properties are also displayed in the property browser and can be folded.
Next I'll write a simple control to illustrate the control. The code is as follows:
using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Windows.Forms;
Using System.Drawing;
Using System.ComponentModel;
Using System.Collections; Namespace Customcontrolsample {public class MyListControl:System.Windows.Forms.Control {private List
<Int32> _list = new list<int32> ();
Public Mylistcontrol () {} [Browsable (True)] public list<int32> Item
{get {return _list;
} set {_list = value; } protected override void OnPaint (PaintEventArgs e) {base.
OnPaint (e);
Graphics g = e.graphics;
Draws the border of the control G.drawrectangle (pens.black,new Rectangle (point.empty,new Size (size.width-1,size.height-1))); for (Int32 i = 0; i < _list. Count;
i++) {g.drawstring (_list[i].
ToString (), Font, brushes.black,1, I * fontheight); "}}}