<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      WinForm控件開發(fā)總結(jié)(十)-----為屬性設(shè)置默認值

            本系列的前面幾篇文章講解了如何來定義屬性以及更有效的編輯屬性,接下來我要講一下控件屬性的默認值。如果我們希望自己開發(fā)的控件更易于被其它開發(fā)者使用,那么提供默認值是非常值得的。
            如果你為屬性設(shè)定了默認值,那么當開發(fā)者修改了屬性的值,這個值在Property Explorer中將會以粗體顯示。VS為屬性提供一個上下文菜單,允許程序員使用控件把值重置為默認值。當VS進行控件的串行化時,他會判斷那些值不是默認值,只有不是默認值的屬性才會被串行化,所以為屬性提供默認值時可以大大減少串行化的屬性數(shù)目,提高效率。
            那么VS怎么知道我們的屬性值不是默認值了呢?我們需要一種機制來通知VS默認值。實現(xiàn)這種機制有兩種方法:
            對于簡單類型的屬性,比如Int32Boolean等等這些Primitive類型,你可以在屬性的聲明前設(shè)置一個DefaultValueAttribute,在Attribute的構(gòu)造函數(shù)里傳入默認值。
            對于復(fù)雜的類型,比如FontColor,你不能夠直接將這些類型的值傳遞給Attibute的構(gòu)造函數(shù)。相反你應(yīng)該提供Reset<PropertyName> ShouldSerialize<PropertyName>方法,比如ResetBackgroundColor(),ShouldSerializeBackgroundColor()VS能夠根據(jù)方法的名稱來識別這種方法,比如Reset<PropertyName>方法把重置為默認值,ShouldSerialize<PropertyName>方法檢查屬性是否是默認值。過去我們把它稱之為魔術(shù)命名法,應(yīng)該說是一種不好的編程習(xí)慣,可是現(xiàn)在微軟依然使用這種機制。我還是以前面幾篇文章使用的例子代碼。
            
      using System;
      using System.Collections.Generic;
      using System.Text;
      using System.Windows.Forms;
      using System.ComponentModel;
      using System.Drawing;

      namespace CustomControlSample
      {
          
      public class FirstControl : Control
          
      {

      private String _displayText=”Hello World!”;
      private Color _textColor=Color.Red;

        
      public FirstControl()
              
      {

              }


              
      // ContentAlignment is an enumeration defined in the System.Drawing
              
      // namespace that specifies the alignment of content on a drawing 
              
      // surface.
              private ContentAlignment alignmentValue = ContentAlignment.MiddleLeft;

              [
              Category(
      "Alignment"),
              Description(
      "Specifies the alignment of text.")
              ]
              
      public ContentAlignment TextAlignment
              
      {

                  
      get
                  
      {
                      
      return alignmentValue;
                  }

                  
      set
                  
      {
                      alignmentValue 
      = value;

                      
      // The Invalidate method invokes the OnPaint method described 
                      
      // in step 3.
                      Invalidate();
                  }

              }



       [Browsable(
      true)]
       [DefaultValue(“Hello World”)]
       
      public String DisplayText
      {
      get
      {
      return _displayText;
      }

      set
      {
           _displayText 
      =value;
          Invalidate();
      }

      }


      [Browsable(
      true)]
      public Color TextColor
      {
      get
      {
          
      return _textColor;
      }

      set
      {
          _textColor
      =value;
      Invalidate();
      }

      }


      public void ResetTextColor()
      {
          TextColor
      =Color.Red;
      }


      public bool ShouldSerializeTextColor()
      {
      return TextColor!=Color.Red;
      }


      protected override void OnPaint(PaintEventArgs e)
              
      {
                  
      base.OnPaint(e);
                  StringFormat style 
      = new StringFormat();
                  style.Alignment 
      = StringAlignment.Near;
                  
      switch (alignmentValue)
                  
      {
                      
      case ContentAlignment.MiddleLeft:
                          style.Alignment 
      = StringAlignment.Near;
                          
      break;
                      
      case ContentAlignment.MiddleRight:
                          style.Alignment 
      = StringAlignment.Far;
                          
      break;
                      
      case ContentAlignment.MiddleCenter:
                          style.Alignment 
      = StringAlignment.Center;
                          
      break;
                  }


                  
      // Call the DrawString method of the System.Drawing class to write   
                  
      // text. Text and ClientRectangle are properties inherited from
                  
      // Control.
                  e.Graphics.DrawString(
                      DisplayText,
                      Font,
                      
      new SolidBrush(TextColor),
                      ClientRectangle, style);

              }

          }

      }

            在上面的代碼中,我增加了兩個屬性,一個是DisplayText,這是一個簡單屬性,我們只需要在它的聲明前添加一個DefaultValue Attribute就可以了。另外一個是TextColor屬性,這個復(fù)雜類型的屬性,所以我們提供了ResetTextColorShouldSerializeTextColor來實現(xiàn)默認值。
            默認值的實現(xiàn)就講完了,但是有一點不要忽視了,你設(shè)定了默認值,就應(yīng)該相應(yīng)的初始化這些屬性,比如我們例子中的代碼:
            

      private String _displayText=”Hello World!”;
      private Color _textColor=Color.Red;
      posted @ 2006-12-24 22:21  綸巾客  閱讀(10818)  評論(11)    收藏  舉報
      主站蜘蛛池模板: 午夜福利92国语| 久久综合色天天久久综合图片| 亚洲国产无线乱码在线观看| av老司机亚洲精品天堂| 亚洲av永久无码精品网站| 亚洲精品国产电影| 精品视频一区二区福利午夜| 国产av一区二区不卡| 国精偷拍一区二区三区| 国产精品久久毛片| 亚洲欧美日韩尤物AⅤ一区| 国产日韩一区二区四季| 国产伦一区二区三区精品| 亚洲精品日本久久久中文字幕| 年轻女教师hd中字3| 蜜臀av日韩精品一区二区| 国产成人精品午夜2022| 久久精品熟妇丰满人妻久久| 亚洲欧美自偷自拍视频图片| 最新国产精品亚洲| 少妇高潮潮喷到猛进猛出小说| 99久久无色码中文字幕| 国产不卡一区不卡二区| 亚洲第一精品一二三区| 亚洲熟妇自偷自拍另欧美| 亚洲人成电影在线天堂色| 欧美一本大道香蕉综合视频| 亚洲国产一区二区三区| 国产福利微视频一区二区| 国产欧美精品一区二区三区-老狼| 亚洲国产成人自拍视频网| 国产精品久久久久影院色| 97一期涩涩97片久久久久久久| 97成人碰碰久久人人超级碰oo| 中文字幕亚洲综合久久综合| 蜜臀在线播放一区在线播放| 国产性一交一乱一伦一色一情| 石家庄市| 日本一区二区三区在线播放| 极品少妇被后入内射视| 久久久久青草线蕉亚洲|