DesignerAutoFormat 類別

定義

提供抽象基底類別,以便建立可在設計階段套用到自訂 Web 伺服器控制項的格式。

public ref class DesignerAutoFormat abstract
public abstract class DesignerAutoFormat
type DesignerAutoFormat = class
Public MustInherit Class DesignerAutoFormat
繼承
DesignerAutoFormat

範例

下列程式碼範例示範如何在自訂控制項設計工具中實作自動格式設定。 衍生控制項設計工具會新增衍生自 DesignerAutoFormat 類別之自訂自動格式的三個實例,以實 AutoFormats 作 屬性。

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.Design;
using System.Web.UI.Design.WebControls;
using System.Web.UI.WebControls;

namespace CustomControls.Design.CS
{
    // A custom Label control whose contents can be indented
    [Designer(typeof(IndentLabelDesigner)), 
        ToolboxData("<{0}:IndentLabel Runat=\"server\"></{0}:IndentLabel>")]
    public class IndentLabel : Label
    {
        private int _indent = 0;

        // Property to indent all text within the label
        [Category("Appearance"), DefaultValue(0), 
            PersistenceMode(PersistenceMode.Attribute)]
        public int Indent
        {
            get { return _indent; }
            set
            {
                _indent = value;
                // Get the number of pixels to indent
                int ind = value * 8;

                //  Add the indent style to the control
                if (ind > 0)
                    this.Style.Add(HtmlTextWriterStyle.MarginLeft, ind.ToString() + "px");
                else
                    this.Style.Remove(HtmlTextWriterStyle.MarginLeft);
            }
        }
    }

    // A design-time ControlDesigner for the IndentLabel control
    [SupportsPreviewControl(true)]
    public class IndentLabelDesigner : LabelDesigner
    {
        private DesignerAutoFormatCollection _autoFormats = null;

        // The collection of AutoFormat objects for the IndentLabel object
        public override DesignerAutoFormatCollection AutoFormats
        {
            get
            {
                if (_autoFormats == null)
                {
                    // Create the collection
                    _autoFormats = new DesignerAutoFormatCollection();

                    // Create and add each AutoFormat object
                    _autoFormats.Add(new IndentLabelAutoFormat("MyClassic"));
                    _autoFormats.Add(new IndentLabelAutoFormat("MyBright"));
                    _autoFormats.Add(new IndentLabelAutoFormat("Default"));
                }
                return _autoFormats;
            }
        }

        // An AutoFormat object for the IndentLabel control
        private class IndentLabelAutoFormat : DesignerAutoFormat
        {
            public IndentLabelAutoFormat(string name) : base(name)
            { }

            // Applies styles based on the Name of the AutoFormat
            public override void Apply(Control inLabel)
            {
                if (inLabel is IndentLabel)
                {
                    IndentLabel ctl = (IndentLabel)inLabel;

                    // Apply formatting according to the Name
                    if (this.Name == "MyClassic")
                    {
                        // For MyClassic, apply style elements directly to the control
                        ctl.ForeColor = Color.Gray;
                        ctl.BackColor = Color.LightGray;
                        ctl.Font.Size = FontUnit.XSmall;
                        ctl.Font.Name = "Verdana,Geneva,Sans-Serif";
                    }
                    else if (this.Name == "MyBright")
                    {
                        // For MyBright, apply style elements to the Style property
                        this.Style.ForeColor = Color.Maroon;
                        this.Style.BackColor = Color.Yellow;
                        this.Style.Font.Size = FontUnit.Medium;

                        // Merge the AutoFormat style with the control's style
                        ctl.MergeStyle(this.Style);
                    }
                    else
                    {
                        // For the Default format, apply style elements to the control
                        ctl.ForeColor = Color.Black;
                        ctl.BackColor = Color.Empty;
                        ctl.Font.Size = FontUnit.XSmall;
                    }
                }
            }
        }
    }
}
Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.Web.UI
Imports System.Web.UI.Design
Imports System.Web.UI.Design.WebControls
Imports System.Web.UI.WebControls

Namespace CustomControls.Design

    ' A custom Label control whose contents can be indented
    <Designer(GetType(IndentLabelDesigner)), _
        ToolboxData("<{0}:IndentLabel Runat=""server""></{0}:IndentLabel>")> _
    Public Class IndentLabel
        Inherits System.Web.UI.WebControls.Label

        Dim _indent As Integer = 0

        <Category("Appearance"), DefaultValue(0), _
            PersistenceMode(PersistenceMode.Attribute)> _
        Property Indent() As Integer
            Get
                Return _indent
            End Get
            Set(ByVal Value As Integer)
                _indent = Value

                ' Get the number of pixels to indent
                Dim ind As Integer = _indent * 8

                ' Add the indent style to the control
                If ind > 0 Then
                    Me.Style.Add(HtmlTextWriterStyle.MarginLeft, ind.ToString() & "px")
                Else
                    Me.Style.Remove(HtmlTextWriterStyle.MarginLeft)
                End If
            End Set
        End Property

    End Class

    ' A design-time ControlDesigner for the IndentLabel control
    Public Class IndentLabelDesigner
        Inherits LabelDesigner

        Private _autoFormats As DesignerAutoFormatCollection = Nothing
        ' The collection of AutoFormat objects for the IndentLabel object
        Public Overrides ReadOnly Property AutoFormats() As DesignerAutoFormatCollection
            Get
                If _autoFormats Is Nothing Then
                    ' Create the collection
                    _autoFormats = New DesignerAutoFormatCollection()

                    ' Create and add each AutoFormat object
                    _autoFormats.Add(New IndentLabelAutoFormat("MyClassic"))
                    _autoFormats.Add(New IndentLabelAutoFormat("MyBright"))
                    _autoFormats.Add(New IndentLabelAutoFormat("Default"))
                End If

                Return _autoFormats
            End Get
        End Property

        ' An AutoFormat object for the IndentLabel control
        Public Class IndentLabelAutoFormat
            Inherits DesignerAutoFormat

            Public Sub New(ByVal name As String)
                MyBase.New(name)
            End Sub

            ' Applies styles based on the Name of the AutoFormat
            Public Overrides Sub Apply(ByVal inLabel As Control)
                If TypeOf inLabel Is IndentLabel Then
                    Dim ctl As IndentLabel = CType(inLabel, IndentLabel)

                    ' Apply formatting according to the Name
                    If Me.Name.Equals("MyClassic") Then
                        ' For MyClassic, apply style elements directly to the control
                        ctl.ForeColor = Color.Gray
                        ctl.BackColor = Color.LightGray
                        ctl.Font.Size = FontUnit.XSmall
                        ctl.Font.Name = "Verdana,Geneva,Sans-Serif"
                    ElseIf Me.Name.Equals("MyBright") Then
                        ' For MyBright, apply style elements to the Style object
                        Me.Style.ForeColor = Color.Maroon
                        Me.Style.BackColor = Color.Yellow
                        Me.Style.Font.Size = FontUnit.Medium

                        ' Merge the AutoFormat style with the control's style
                        ctl.MergeStyle(Me.Style)
                    Else
                        ' For the Default format, apply style elements to the control
                        ctl.ForeColor = Color.Black
                        ctl.BackColor = Color.Empty
                        ctl.Font.Size = FontUnit.XSmall
                    End If
                End If
            End Sub
        End Class
    End Class

End Namespace

備註

DesignerAutoFormat 提供基類,可在 Visual Studio 2005 等視覺化設計工具的設計階段繼承和擴充,在設計階段顯示格式化的自訂 Web 服務器控制項。

控制項開發人員會提供自動格式設定,以協助使用控制項的開發人員選取慣用的顯示器。 自訂 DesignerAutoFormat 物件會與自訂控制項設計工具搭配運作,以在設計階段為自訂控制項提供自動格式設定。 例如, Calendar 控制項提供開發人員在設計階段可以套用的各種格式。

若要實作自訂控制項的自動格式設定:

  1. 建立自訂控制項。

  2. ControlDesigner 類別或適用于控制項的另一個設計工具類別衍生設計工具類別,例如 TextControlDesigner

  3. 藉由覆寫 方法,從 DesignerAutoFormat 格式化自訂控制項的 Apply 類別衍生格式類別。

  4. 在您的設計工具類別中,針對 AutoFormats 設計工具可以套用的每個具名格式,填入 屬性,也就是 物件,其中一個 DesignerAutoFormatCollection 格式類別的實例。

類別 DesignerAutoFormat 提供下列成員,以支援設計階段的自動格式設定:

  • 方法 Apply ,會將具名格式套用至指定的控制項。

  • GetPreviewControl方法,提供控制項複本,可在 Visual Studio 2005 等視覺化設計工具的[自動格式設定] 對話方塊中預覽。

  • Name屬性,提供在視覺化設計工具中以格式清單顯示的文字。

給實施者的注意事項

當您繼承自 DesignerAutoFormat 類別時,必須覆寫 Apply(Control) 方法來預覽格式化的控制項,並將選取的格式套用至控制項。

建構函式

DesignerAutoFormat(String)

初始化 DesignerAutoFormat 類別的新執行個體。

屬性

Name

取得 DesignerAutoFormat 物件的名稱。

Style

取得 DesignerAutoFormatStyle 物件,由 DesignerAutoFormat 物件用於呈現關聯之控制項的設計階段預覽。

方法

Apply(Control)

將關聯的格式設定套用到指定的控制項。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetPreviewControl(Control)

傳回關聯之控制項的複本,以便在將格式套用至控制項之前進行預覽。

GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回表示目前 DesignerAutoFormat 物件的字串。

適用於

另請參閱