Summary of WinForm Control Development (iv) serialization of control properties

Source: Internet
Author: User
Tags generator serialization

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); "}}} 

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.