C#中怎么設(shè)置WinForm控件

C# 中怎么設(shè)置WinForm控件,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

創(chuàng)新互聯(lián)建站專注于南岳網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供南岳營銷型網(wǎng)站建設(shè),南岳網(wǎng)站制作、南岳網(wǎng)頁設(shè)計(jì)、南岳網(wǎng)站官網(wǎng)定制、重慶小程序開發(fā)服務(wù),打造南岳網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供南岳網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。

如果你為屬性設(shè)定了默認(rèn)值,那么當(dāng)開發(fā)者修改了屬性的值,這個(gè)值在Property Explorer中將會(huì)以粗體顯示。VS為屬性提供一個(gè)上下文菜單,允許程序員使用C# WinForm控件開發(fā)把值重置為默認(rèn)值。

當(dāng)Visual Studio進(jìn)行控件的串行化時(shí),他會(huì)判斷那些值不是默認(rèn)值,只有不是設(shè)置默認(rèn)值的屬性才會(huì)被串行化,所以為屬性提供設(shè)置默認(rèn)值時(shí)可以大大減少串行化的屬性數(shù)目,提高效率。

那么Visual Studio進(jìn)怎么知道我們的屬性值不是默認(rèn)值了呢?我們需要一種機(jī)制來通知Visual Studio進(jìn)默認(rèn)值。實(shí)現(xiàn)這種機(jī)制有兩種方法:

對(duì)于簡單類型的屬性,比如Int32,Boolean等等這些Primitive類型,你可以在屬性的聲明前設(shè)置一個(gè)DefaultValueAttribute,在Attribute的構(gòu)造函數(shù)里傳入設(shè)置默認(rèn)值。

對(duì)于復(fù)雜的類型,比如Font,Color,你不能夠直接將這些類型的值傳遞給Attibute的構(gòu)造函數(shù)。相反你應(yīng)該提供Reset和ShouldSerialize方法,比如ResetBackgroundColor(),ShouldSerializeBackgroundColor()。

VS能夠根據(jù)方法的名稱來識(shí)別這種方法,比如Reset方法把重置為設(shè)置默認(rèn)值,ShouldSerialize方法檢查屬性是否是設(shè)置默認(rèn)值。過去我們把它稱之為魔術(shù)命名法,應(yīng)該說是一種不好的編程習(xí)慣,可是現(xiàn)在微軟依然使用這種機(jī)制。我還是以前面幾篇文章使用的例子代碼。

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

在上面C# WinForm控件開發(fā)的代碼中,我增加了兩個(gè)屬性,一個(gè)是DisplayText,這是一個(gè)簡單屬性,我們只需要在它的聲明前添加一個(gè)DefaultValue Attribute就可以了。

另外一個(gè)是TextColor屬性,這個(gè)復(fù)雜類型的屬性,所以我們提供了ResetTextColor和ShouldSerializeTextColor來實(shí)現(xiàn)默認(rèn)值。

C# WinForm控件開發(fā)設(shè)置默認(rèn)值的實(shí)現(xiàn)就講完了,但是有一點(diǎn)不要忽視了,你已經(jīng)設(shè)置默認(rèn)值,就應(yīng)該相應(yīng)的初始化這些屬性,比如我們例子中的代碼:

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

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。

分享標(biāo)題:C#中怎么設(shè)置WinForm控件
本文路徑:http://muchs.cn/article20/gjgdco.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、定制開發(fā)、Google、動(dòng)態(tài)網(wǎng)站、搜索引擎優(yōu)化、小程序開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)