自定义控件的 Enum类和Color类 属性的公开设定


我们知道 常规状态下
自定义控件经常使用String,Boolean型的属性
这些属性公开后 在使用时 就可以看到可输入的文本框
及有true和false供选择的下拉列表

可是我们有时还要用到 更多内容的下拉列表 或 颜色选择框
那么 下面就是
自定义控件的 Enum类和Color类 属性的公开设定 的示例
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace WebControlLibrary3
{
 /// <summary>
 /// WebCustomControl1 的摘要描述。
 /// </summary>
 [DefaultProperty("Text"),
  ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")]
 public class WebCustomControl1 : System.Web.UI.WebControls.WebControl
 {
  private string _text;
  private Button _button;
  private System.Drawing.Color _color;


  [Bindable(true),
   Category("Appearance"),
   DefaultValue("default_text"),
  Description("文本属性,显示在属性样下边提示部分")]
  public string Text
  {
   get
   {
    return _text;
   }

   set
   {
    _text = value;
   }
  }

  // 枚举
  public enum TextStyle
  {
   Normal,
   Italic,
   Bold,  
  }

     private TextStyle _textStyle;
  public TextStyle textStyle
  {
   get
   {
   return _textStyle;
   }
   set
   {
   _textStyle = value;
   }
  }
        // 系统颜色对话框

  [Bindable(true),
  Category("Appearance"),
  DefaultValue("Red"),
  Description("按钮背景颜色")]
  public System.Drawing.Color ButtonColor
  {
   get
   {
               return _color;

   }
   set
   {
      _color = value;
   }
  }
       

  /// <summary>
  /// 呈现这个控制项到 output 参数所指定的物件。
  /// </summary>
  /// <param name="output"> 指定要输出的 HTML 写入器 (HTMLTextWriter)</param>
  protected override void Render(HtmlTextWriter output)
  {
   string StartStyle = null;
   string EndStyle = null;
   switch(this._textStyle)
   {
    case TextStyle.Normal:
     StartStyle="";
                    EndStyle="";
     break;
    case TextStyle.Italic:
     StartStyle="<I>";
     EndStyle="</I>";
     break;
   
    case TextStyle.Bold:
     StartStyle="<B>";
     EndStyle="</B>";
     break;   
   
   }   
   output.Write(StartStyle+Text+EndStyle);
   _button = new Button();
   _button.Text = "测试Color按钮";
   _button.BackColor = this._color;
   _button.RenderControl(output);
  }
 }
}

posted on 2006-12-14 13:14  freeliver54  阅读(832)  评论(1编辑  收藏  举报

导航