DesignerAutoFormat Classe

Definizione

Fornisce la classe base astratta per la creazione di formati applicabili a un controllo server Web personalizzato in fase di progettazione.

public ref class DesignerAutoFormat abstract
public abstract class DesignerAutoFormat
type DesignerAutoFormat = class
Public MustInherit Class DesignerAutoFormat
Ereditarietà
DesignerAutoFormat

Esempio

Nell'esempio di codice seguente viene illustrato come implementare la formattazione automatica in una finestra di progettazione controlli personalizzata. Progettazione controlli derivati implementa la AutoFormats proprietà aggiungendo tre istanze di un formato automatico personalizzato derivato dalla DesignerAutoFormat classe .

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

Commenti

DesignerAutoFormat fornisce una classe di base che può essere ereditata e estesa per visualizzare un controllo server Web personalizzato formattato in fase di progettazione in uno strumento di progettazione visiva, ad esempio Visual Studio 2005.

Uno sviluppatore di controlli fornisce la formattazione automatica per aiutare gli sviluppatori che usano il controllo per selezionare una visualizzazione preferita. Un oggetto personalizzato DesignerAutoFormat funziona con una finestra di progettazione controlli personalizzata per fornire la formattazione automatica in fase di progettazione per un controllo personalizzato. Ad esempio, il Calendar controllo fornisce un'ampia gamma di formati che possono essere applicati da uno sviluppatore in fase di progettazione.

Per implementare la formattazione automatica per un controllo personalizzato:

  1. Creare il controllo personalizzato.

  2. Derivare una classe della finestra di progettazione dalla classe o da un'altra classe di ControlDesigner progettazione appropriata per il controllo, ad esempio TextControlDesigner.

  3. Derivare una classe di formato dalla DesignerAutoFormat classe che formatta il controllo personalizzato eseguendo l'override del Apply metodo .

  4. Nella classe di progettazione popolare la AutoFormats proprietà , ovvero un DesignerAutoFormatCollection oggetto , con un'istanza della classe di formato per ogni formato denominato che la finestra di progettazione può applicare.

La DesignerAutoFormat classe fornisce i membri seguenti per supportare la formattazione automatica in fase di progettazione:

  • Metodo Apply , che applica il formato denominato al controllo specificato.

  • Metodo GetPreviewControl , che fornisce una copia del controllo per l'anteprima in una finestra di dialogo Formattazione automatica di una finestra di progettazione visiva, ad esempio Visual Studio 2005.

  • Proprietà Name , che fornisce il testo da visualizzare in un elenco di formati in una finestra di progettazione visiva.

Note per gli implementatori

Quando si eredita dalla DesignerAutoFormat classe , è necessario eseguire l'override del Apply(Control) metodo per visualizzare in anteprima un controllo formattato e applicare il formato selezionato al controllo.

Costruttori

DesignerAutoFormat(String)

Inizializza una nuova istanza della classe DesignerAutoFormat.

Proprietà

Name

Ottiene il nome di un oggetto DesignerAutoFormat.

Style

Ottiene un oggetto DesignerAutoFormatStyle utilizzato dall'oggetto DesignerAutoFormat per eseguire il rendering di un'anteprima in fase di progettazione del controllo associato.

Metodi

Apply(Control)

Applica la formattazione associata al controllo specificato.

Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.

(Ereditato da Object)
GetHashCode()

Funge da funzione hash predefinita.

(Ereditato da Object)
GetPreviewControl(Control)

Restituisce una copia del controllo associato per fornire un'anteprima prima di applicare il formato al controllo.

GetType()

Ottiene l'oggetto Type dell'istanza corrente.

(Ereditato da Object)
MemberwiseClone()

Crea una copia superficiale dell'oggetto Object corrente.

(Ereditato da Object)
ToString()

Restituisce una stringa che rappresenta l'oggetto DesignerAutoFormat corrente.

Si applica a

Vedi anche