用户自定义控件

复合控件封装有一个可以作为控件重复使用的用户界面。可视化设计器为创建复合控件提供了有力的支持。要创作一个派生自System.Windows.Forms.UserControl 的复合控件。基类 UserControl 为子控件提供了键盘路由并使子控件可以作为一个组进行工作。

·  扩展现有控件,对其进行自定义或为其添加功能。

可以通过从任何 Windows 窗体控件派生控件并重写或添加属性、方法和事件的方式来自定义 Windows 窗体控件。

· 创作一个不是通过组合或扩展现有控件而形成的控件。

在这种方案中,需从基类 System.Windows.Forms.Control 派生控件。可以添加和重写基类的属性、方法和事件,来制作功能强大,能满足自己需求的控件。

Windows 窗体控件的基类 System.Windows.Forms.Control 为客户端 Windows 应用程序中的外观显示提供了所需的途径。Control 提供了一个窗口句柄,用来处理消息路由并提供鼠标和键盘事件及许多其他用户界面事件。还提供了高级布局,并具有用于外观显示的特定属性,如ForeColor、BackColor、Height、Width 和许多其他属性。此外,它还提供了安全性、线程支持以及与 ActiveX 控件的交互性。由于基类提供了很多基础结构,使得开发自己的 Windows 窗体控件变得相对简单。

 下面是我写的一个搜索控件:

=============================================================

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace HelloW3.PortalControls
{

    [DefaultProperty("Text")]
    [ToolboxData("<{0}:SearchControl runat=server></{0}:SearchControl>")]
    public class SearchControl : CompositeControl
    {
        #region Property

        DropDownList drpSearchField;
        TextBox txtSearch;
        Button btnSearch;
        public event EventHandler SearchEvent;
        #endregion

        /// <summary>
        /// 搜索文本
        /// </summary>
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                this.EnsureChildControls();
                return this.txtSearch.Text;
            }

            set
            {
                this.EnsureChildControls();
                this.txtSearch.Text = Text;  
            }
        }

        /// <summary>
        /// 搜索栏标题
        /// </summary>
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Title
        {
            get
            {
                String s = (String)ViewState["title"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["title"] = value;
            }
        }

        /// <summary>
        /// 搜索栏排列方式
        /// </summary>
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue(OrientationType.Vertical)]
        [Localizable(true)]
        public OrientationType Orientation
        {
            get
            {
                return this.ViewState["orientate"]==null?OrientationType.Vertical:(OrientationType)ViewState["orientate"];
            }

            set
            {
                ViewState["orientate"] = value;
            }
        }

        /// <summary>
        /// 搜索栏排列方式
        /// </summary>
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue(0)]
        [Localizable(true)]
        public int Border
        {
            get
            {
                return this.ViewState["border"] == null ? 1 : (int)this.ViewState["border"];
            }

            set
            {
                this.ViewState["border"] = value;
            }
        }

        /// <summary>
        /// 搜索栏排列方式
        /// </summary>
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue(true)]
        [Localizable(true)]
        public bool EnabelStyle
        {
            get
            {
                return this.ViewState["enablestyle"] == null ? true : (bool)this.ViewState["enablestyle"];
            }

            set
            {
                this.ViewState["enablestyle"] = value;
            }
        }

        public string SearchClientClick
        {
            get { return this.btnSearch.OnClientClick; }
            set { this.btnSearch.OnClientClick = value; }
        }

        /// <summary>
        /// 下拉框绑定的显示文字字段
        /// </summary>
        [Bindable(true)]
        [Category("Data")]
        [DefaultValue(null)]
        public string DataTextField
        {
            get
            {
                this.EnsureChildControls();
                return this.drpSearchField.DataTextField;
            }
            set
            {
                this.EnsureChildControls();
                this.drpSearchField.DataTextField = value;
            }
        }

        /// <summary>
        /// 下拉框绑定的值字段
        /// </summary>
        [Bindable(true)]
        [Category("Data")]
        [DefaultValue(null)]
        public string DataValueField
        {
            get
            {
                this.EnsureChildControls();
                return this.drpSearchField.DataValueField;
            }
            set
            {
                this.EnsureChildControls();
                this.drpSearchField.DataValueField = value;
            }
        }

        /// <summary>
        /// 下拉框数据源
        /// </summary>
        [Bindable(true)]
        [Category("Data")]
        [DefaultValue(null)]
        public object SearchDataSource
        {
            get
            {
                this.EnsureChildControls();
                return this.drpSearchField.DataSource;
            }
            set
            {
                this.EnsureChildControls();
                this.drpSearchField.DataSource = value;
            }
        }

        /// <summary>
        /// 获取或设置选定的下拉框文本
        /// </summary>
        [Bindable(true)]
        [Category("Data")]
        [DefaultValue(null)]
        public string SelectFieldText
        {
            get
            {
                this.EnsureChildControls();
                return this.drpSearchField.Text;
            }
            set
            {
                this.EnsureChildControls();
                this.drpSearchField.Text = value;
            }
        }

        /// <summary>
        /// 获取或设置选定的下拉框的值
        /// </summary>
        [Bindable(true)]
        [Category("Data")]
        [DefaultValue(null)]
        public string SelectFieldValue
        {
            get
            {
                this.EnsureChildControls();
                return this.drpSearchField.SelectedValue;           
            }
            set
            {
                this.EnsureChildControls();
                this.drpSearchField.SelectedValue = value;
            }
        }

        /// <summary>
        /// 获取或设置选定的下拉框选定索引
        /// </summary>
        [Bindable(true)]
        [Category("Data")]
        [DefaultValue(null)]
        public int SelectFieldIndex
        {
            get
            {
                this.EnsureChildControls();
                return this.drpSearchField.SelectedIndex;
            }
            set
            {
                this.EnsureChildControls();
                this.drpSearchField.SelectedIndex = value;
            }
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            this.btnSearch = new Button();
            this.btnSearch.ID = "btnSearch";
            this.txtSearch = new TextBox();
            this.drpSearchField = new DropDownList();
            this.btnSearch.Click += new EventHandler(SearchClick);
            this.Controls.Add(btnSearch);
            this.Controls.Add(txtSearch);
            this.Controls.Add(drpSearchField);
        }


        protected override void RenderContents(HtmlTextWriter output)
        {
            //HtmlTable table = new HtmlTable();
            if (EnabelStyle)
            output.AddAttribute(HtmlTextWriterAttribute.Class, "list1 ListBG1 border_All");
            //if(EnabelStyle)
            //table.Attributes["class"] = "list1 ListBG1 border_All";
            output.AddAttribute(HtmlTextWriterAttribute.Width, "100%");
            //table.Width = "100%";
            //output.AddAttribute(HtmlTextWriterAttribute.Border, this.Border.ToString());
            output.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, this.Border.ToString());
            //table.Border = this.Border ;
            //table.Style.Add(HtmlTextWriterStyle.BorderWidth, this.Border.ToString());
            output.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "0");
            output.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0");
            //table.CellPadding = 0;
            //table.CellSpacing = 0;
            //table.Border = 0; 
            output.RenderBeginTag(HtmlTextWriterTag.Table);           
            //HtmlTableRow row;            
            //HtmlTableCell cell;
            if (!string.IsNullOrEmpty(this.Title))
            {
                output.RenderBeginTag(HtmlTextWriterTag.Tr);
                //row = new HtmlTableRow();
                //cell = new HtmlTableCell("th");
                output.AddAttribute(HtmlTextWriterAttribute.Colspan, "3");
                output.AddAttribute(HtmlTextWriterAttribute.Class, "left3");
                output.RenderBeginTag(HtmlTextWriterTag.Th);
                output.Write(string.Format("<h3 class='H3style1'>{0}</h3>", this.Title));
                //cell.Attributes["colspan"] = "3";
                //cell.Attributes["class"] = "left3";
                //cell.InnerHtml = string.Format("<h3 class='H3style1'>{0}</h3>", this.Title);
                //row.Cells.Add(cell);
                //table.Rows.Add(row);
                output.RenderEndTag();
                output.RenderEndTag();
            }
            //table.Rows.Add(LineRow());
            LineRow(output);
            //row = new HtmlTableRow();
            //cell = new HtmlTableCell();
            output.RenderBeginTag(HtmlTextWriterTag.Tr);
            btnSearch.Attributes["class"] = "searchBTN2";
            btnSearch.Text = "搜索";
            if (OrientationType.Vertical == this.Orientation)//垂直方向
            {
                output.AddAttribute(HtmlTextWriterAttribute.Align, "center");
                output.AddAttribute(HtmlTextWriterAttribute.Class, "selectLMDH");
                output.RenderBeginTag(HtmlTextWriterTag.Td);
                //cell.Align = "center";
                //cell.Attributes["class"] = "selectLMDH";               
                //cell.Controls.Add(drpSearchField);
                output.AddAttribute(HtmlTextWriterAttribute.Width, "100%");
                this.drpSearchField.RenderControl(output);
                output.RenderEndTag();
                //row.Cells.Add(cell);
                //cell = new HtmlTableCell();
                //cell.Align = "center";
                //cell.RowSpan = 2;
                output.AddAttribute(HtmlTextWriterAttribute.Align, "center");
                output.AddAttribute(HtmlTextWriterAttribute.Rowspan, "2");
                //cell.Controls.Add(btnSearch);
                output.RenderBeginTag(HtmlTextWriterTag.Td);
                output.AddAttribute(HtmlTextWriterAttribute.Width, "100%");
                btnSearch.RenderControl(output);
                output.RenderEndTag();
                output.RenderEndTag();
                output.RenderBeginTag(HtmlTextWriterTag.Tr);
                output.AddAttribute(HtmlTextWriterAttribute.Class, "selectLMDH");
                output.AddAttribute(HtmlTextWriterAttribute.Align, "center");
                output.RenderBeginTag(HtmlTextWriterTag.Td);
                txtSearch.RenderControl(output);
                output.RenderEndTag();
                //row.Cells.Add(cell);
                //table.Rows.Add(row);
                //row = new HtmlTableRow();
                //cell = new HtmlTableCell();
                //cell.Attributes["class"] = "selectLMDH";
                //cell.Align = "center";               
                //cell.Controls.Add(txtSearch);
                //row.Cells.Add(cell);
                //table.Rows.Add(row);
            }
            else//水平方向
            {
                output.AddAttribute(HtmlTextWriterAttribute.Class, "selectLMDH");
                output.AddAttribute(HtmlTextWriterAttribute.Align, "center");
                output.RenderBeginTag(HtmlTextWriterTag.Td);
                //cell.Align = "center";
                //cell.Attributes["class"] = "selectLMDH";
                drpSearchField.Attributes["class"] = "searchSelect";
                drpSearchField.RenderControl(output);
                //cell.Controls.Add(drpSearchField);
                //row.Cells.Add(cell);
                output.RenderEndTag();
                output.AddAttribute(HtmlTextWriterAttribute.Class, "selectLMDH");
                output.AddAttribute(HtmlTextWriterAttribute.Align, "left");
                output.RenderBeginTag(HtmlTextWriterTag.Td);
                //cell = new HtmlTableCell();
                //cell.Align = "left";
                txtSearch.Attributes["class"] = "searchText";
                txtSearch.RenderControl(output);
                //cell.Controls.Add(txtSearch);
                //row.Cells.Add(cell);
                output.RenderEndTag();
                output.RenderBeginTag(HtmlTextWriterTag.Td);
                btnSearch.RenderControl(output);
                output.RenderEndTag();
                //cell = new HtmlTableCell();
                //cell.Controls.Add(btnSearch);
                //row.Cells.Add(cell);
                //table.Rows.Add(row);
            }
            //table.Rows.Add(LineRow());
            //table.RenderControl(output);
            output.RenderEndTag();
            LineRow(output);
            output.RenderEndTag();
        }

        private void LineRow(HtmlTextWriter output)
        {
            //HtmlTableRow lineRow = new HtmlTableRow();
            //HtmlTableCell lineCell = new HtmlTableCell();
            output.RenderBeginTag(HtmlTextWriterTag.Tr);
            output.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
            output.AddAttribute(HtmlTextWriterAttribute.Class, "listPadding10");
            output.RenderBeginTag(HtmlTextWriterTag.Td);
            output.RenderEndTag();
            output.RenderEndTag();
            //lineCell.ColSpan = 2;
            //lineCell.Attributes["class"] = "listPadding10";
            //lineRow.Cells.Add(lineCell);
            //return lineRow;
        }

        /// <summary>
        /// 触发搜索事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void SearchClick(object sender, EventArgs e)
        {
            if (this.SearchEvent != null)
            {
                SearchEvent(sender, e);
            }
        }

        protected override void OnInit(EventArgs e)
        {          
            base.OnInit(e);           
        }

        public override void DataBind()
        {
            base.DataBind();
            this.drpSearchField.DataBind();
            this.txtSearch.DataBind();
            this.btnSearch.DataBind();
            this.SelectFieldIndex = 0;
        }
    }

    public enum OrientationType
    {
        Horizontal=0,
        Vertical
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值