乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      WinForm控件開發(fā)總結(jié)(四)

       googo 2011-07-19

      WinForm控件開發(fā)總結(jié)(四)-控件屬性的串行化

      2009-07-15  來自:博客園  字體大?。骸?a class="d1" href="javascript:doZoom(16)">大  
      • 摘要:本文介紹自定義控件屬性的串行化(DesignerSerializationVisibilityAttribute)。
      前一篇文章介紹了常用的設(shè)計時Attribute。其中BrowsableAttribute,CategoryAttribute,DescriptionAttribute,DefaultPropertyAttribute,DefaultEventAttribute都是比較簡單的,也是可有可無,但是為了提供更好的用戶體驗這些Attribute最好不要省掉,如果你對這些Attribute還不熟悉,可以參考我前一篇文章的描述或者查看MSDN,這里我就不在贅述了。
             下來我們主要介紹一下DesignerSerializationVisibilityAttributeTypeConverterAttribute。
             DesignerSerializationVisibilityAttribute的功能是指示一個屬性是否串行化和如何串行化,它的值是一個枚舉,一共有三種類型Content,Hidden,Visible。Content指示代碼生成器為對象包含的內(nèi)容生成代碼,而不是為對象本身,Hidden指示代碼生成器不為對象生成代碼,visible指示代碼生成器為對象生成代碼。假如你的控件有一個集合屬性,又想在設(shè)計時自動將集合屬性的內(nèi)容生成代碼,那么就使用這個Attribute,并將值設(shè)為DesignerSerializationVisibility.Content
            TypeConverterAttribute的作用就更大一些,也稍微復(fù)雜一些。TypeConverterAttribute主要的目的是為屬性指定一個類型轉(zhuǎn)換器,這個轉(zhuǎn)化器可以將屬性的值轉(zhuǎn)換城其它的類型。.NET框架已經(jīng)為大部分常用的類型都提供了類型轉(zhuǎn)換器,比如Color就有ColorConverter,枚舉類型就有EnumConverter,等等,所以一般情況下你沒有必要寫類型轉(zhuǎn)換器,如果你的屬性的特殊的類型或者自定義的類型那么就必須要寫了。類型轉(zhuǎn)換器都是從System.ComponentModel.TypeConverter派 生出來的,你需要重寫其中的一些方法來達到轉(zhuǎn)換的目的,在我們開發(fā)的過程中,其實只關(guān)心屬性的值如何轉(zhuǎn)換成字符串(因為屬性的值需要在屬性瀏覽器里顯示出 來,屬性瀏覽器里顯示的都是字符串)和源代碼(需要自動為屬性的值生成源代碼以實現(xiàn)持久化),當(dāng)然反過來,也要將字符串和源代碼轉(zhuǎn)換成屬性的值。另外使用TypeConverter也可以實現(xiàn)子屬性,讓屬性的子屬性也顯示在屬性瀏覽器里,并且可以折疊。
             接下來我就寫一個簡單的控件來演示一下這個控件。代碼如下:
            
      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;
                  
      //繪制控件的邊框

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

              }

          }

      }


            我創(chuàng)建了一個簡單的List控件,將用戶輸入的數(shù)據(jù)顯示在控件中,效果圖如下:
         
           在這個控件中,我聲明了一個集合屬性Item供用戶輸入要顯示的整型數(shù)值。我們按照WinForm控件制作教程(二)中的方法將控件加到ToolBox里,然后拖到Form設(shè)計器中,然后選中控件,在屬性瀏覽中查看控件的屬性,屬性中有一個Item的屬性,屬性右邊的值顯示為Collection,當(dāng)你點擊這個值的時候,值的右邊出現(xiàn)一個小按鈕,點擊這個小按鈕,就會出現(xiàn)彈出一個Collection Editor窗口,你可以在在這個編輯器里添加你想顯示的整型值,如圖:
            
           
      添加完以后,關(guān)閉Collection Editor?,F(xiàn)在我們看看Form設(shè)計器為我們生成了什么代碼。對于用戶在Form設(shè)計器中設(shè)計的內(nèi)容,設(shè)計器的代碼生成器會將代碼生成到窗口類的InitializeComponent()方法中,對于vs2005來說,這個方法位于***.Designer.cs文件中,在我當(dāng)前的工程中位于Form1.Designer.cs文件中。在solution瀏覽器中雙擊打開這個文件,看看Form設(shè)計器為我們生成了什么代碼:
            
                  // 
                  
      // myListControl1
                  
      // 
                  this.myListControl1.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
                  
      this.myListControl1.Item = ((System.Collections.Generic.List<int>)(resources.GetObject("myListControl1.Item")));
                  
      this.myListControl1.Location = new System.Drawing.Point(1234);
                  
      this.myListControl1.Name = "myListControl1";
                  
      this.myListControl1.Size = new System.Drawing.Size(220180);
                  
      this.myListControl1.TabIndex = 1;
                  
      this.myListControl1.Text = "myListControl1";
            

            設(shè)計器將Item的內(nèi)容串行化到了資源文件里。現(xiàn)在我們修改控件的代碼,讓設(shè)計器將Item的內(nèi)容串行化到源代碼里。我們?yōu)?/span>Item屬性添加DesignerSerializationVisibilityAttribute,代碼片斷如下:

            
      [Browsable(true)]
              [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Content)]
              
      public List<Int32> Item
              
      {
                  
      get
                  
      {
                      
      return _list;
                  }

                  
      set
                  
      {
                      _list 
      = value;
                  }

              }

            編輯完以后,Build控件工程,回到測試工程里,將Item屬性里的值,刪掉重新添加,添加完以后,我們再來看看設(shè)計器生成的代碼:
            
      // 
                  
      // myListControl1
                  
      // 
                  this.myListControl1.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
                  
      this.myListControl1.Item.Add(1);
                  
      this.myListControl1.Item.Add(2);
                  
      this.myListControl1.Item.Add(3);
                  
      this.myListControl1.Item.Add(6);
                  
      this.myListControl1.Item.Add(8);
                  
      this.myListControl1.Item.Add(9);
                  
      this.myListControl1.Location = new System.Drawing.Point(1234);
                  
      this.myListControl1.Name = "myListControl1";
                  
      this.myListControl1.Size = new System.Drawing.Size(220180);
                  
      this.myListControl1.TabIndex = 1;
                  
      this.myListControl1.Text = "myListControl1";
            現(xiàn)在設(shè)計器將Item的內(nèi)容串行化到源代碼里了。
            時間有限,今天就寫到這里,下一篇文章我來介紹TypeConverterAttribute。 
      作者:綸巾客

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多