Compartir a través de


Cómo: Ajustar un control de formularios Windows Forms con ToolStripControlHost

Actualización: noviembre 2007

ToolStripControlHost está diseñada para habilitar el alojamiento arbitrario de controles de formularios Windows Forms utilizando el constructor de ToolStripControlHost o extendiendo ToolStripControlHost. Es más sencillo ajustar el control extendiendo ToolStripControlHost e implementando las propiedades y los métodos que exponen las propiedades y métodos del control que más se utilizan. También puede exponer los eventos para el control en el nivel ToolStripControlHost.

Para alojar un control en un ToolStripControlHost por derivación

  1. Extienda ToolStripControlHost. Implemente un constructor predeterminado que llama al constructor de la clase base que se pasa al control deseado.

    ' Call the base constructor passing in a MonthCalendar instance.
    Public Sub New() 
        MyBase.New(New MonthCalendar())
    
    End Sub
    
         // Call the base constructor passing in a MonthCalendar instance.
            public ToolStripMonthCalendar() : base (new MonthCalendar()) { }
    
  2. Declare una propiedad del mismo tipo como el control ajustado y devuelva Control como el tipo correcto de control del descriptor de acceso de la propiedad.

    Public ReadOnly Property MonthCalendarControl() As MonthCalendar 
        Get
            Return CType(Control, MonthCalendar)
        End Get
    End Property
    
         public MonthCalendar MonthCalendarControl
            {
                get
                {
                    return Control as MonthCalendar;
                }
            }
    
  3. Exponga otras propiedades y métodos del control ajustado frecuentemente utilizados con propiedades y métodos de la clase extendida.

    ' Expose the MonthCalendar.FirstDayOfWeek as a property.
    Public Property FirstDayOfWeek() As Day 
        Get
            Return MonthCalendarControl.FirstDayOfWeek
        End Get
        Set
            value = MonthCalendarControl.FirstDayOfWeek
        End Set
    End Property
    
    ' Expose the AddBoldedDate method.
    Public Sub AddBoldedDate(ByVal dateToBold As DateTime) 
        MonthCalendarControl.AddBoldedDate(dateToBold)
    
    End Sub
    
         // Expose the MonthCalendar.FirstDayOfWeek as a property.
            public Day FirstDayOfWeek
            {
                get
                {
                    return MonthCalendarControl.FirstDayOfWeek;
                }
                set { value = MonthCalendarControl.FirstDayOfWeek; }
            }
    
            // Expose the AddBoldedDate method.
            public void AddBoldedDate(DateTime dateToBold)
            {
                MonthCalendarControl.AddBoldedDate(dateToBold);
            }
    
  4. Opcionalmente, reemplace OnSubscribeControlEvents y los métodos OnUnsubscribeControlEvents, y agregue los eventos de control que desea exponer.

    Protected Overrides Sub OnSubscribeControlEvents(ByVal c As Control) 
    
        ' Call the base so the base events are connected.
        MyBase.OnSubscribeControlEvents(c)
    
        ' Cast the control to a MonthCalendar control.
        Dim monthCalendarControl As MonthCalendar = _
            CType(c, MonthCalendar)
    
        ' Add the event.
        AddHandler monthCalendarControl.DateChanged, _
            AddressOf HandleDateChanged
    
    End Sub
    
    Protected Overrides Sub OnUnsubscribeControlEvents(ByVal c As Control)
        ' Call the base method so the basic events are unsubscribed.
        MyBase.OnUnsubscribeControlEvents(c)
    
        ' Cast the control to a MonthCalendar control.
        Dim monthCalendarControl As MonthCalendar = _
            CType(c, MonthCalendar)
    
        ' Remove the event.
        RemoveHandler monthCalendarControl.DateChanged, _
            AddressOf HandleDateChanged
    
    End Sub
    
         protected override void OnSubscribeControlEvents(Control c)
            {
                // Call the base so the base events are connected.
                base.OnSubscribeControlEvents(c);
    
                // Cast the control to a MonthCalendar control.
                MonthCalendar monthCalendarControl = (MonthCalendar) c;
    
                // Add the event.
                monthCalendarControl.DateChanged +=
                    new DateRangeEventHandler(OnDateChanged);
            }
    
            protected override void OnUnsubscribeControlEvents(Control c)
            {
                // Call the base method so the basic events are unsubscribed.
                base.OnUnsubscribeControlEvents(c);
    
                // Cast the control to a MonthCalendar control.
                MonthCalendar monthCalendarControl = (MonthCalendar) c;
    
                // Remove the event.
                monthCalendarControl.DateChanged -=
                    new DateRangeEventHandler(OnDateChanged);
            }
    
  5. Proporcione el ajuste necesario para los eventos que desea exponer.

        ' Declare the DateChanged event.
        Public Event DateChanged As DateRangeEventHandler
    
        ' Raise the DateChanged event.
        Private Sub HandleDateChanged(ByVal sender As Object, _
            ByVal e As DateRangeEventArgs)
    
            RaiseEvent DateChanged(Me, e)
        End Sub
    End Class
    
         // Declare the DateChanged event.
            public event DateRangeEventHandler DateChanged;
    
            // Raise the DateChanged event.
            private void OnDateChanged(object sender, DateRangeEventArgs e)
            {
                if (DateChanged != null)
                {
                    DateChanged(this, e);
                }
            }
    

Ejemplo

'Declare a class that inherits from ToolStripControlHost.

Public Class ToolStripMonthCalendar
    Inherits ToolStripControlHost

    ' Call the base constructor passing in a MonthCalendar instance.
    Public Sub New() 
        MyBase.New(New MonthCalendar())

    End Sub

    Public ReadOnly Property MonthCalendarControl() As MonthCalendar 
        Get
            Return CType(Control, MonthCalendar)
        End Get
    End Property

    ' Expose the MonthCalendar.FirstDayOfWeek as a property.
    Public Property FirstDayOfWeek() As Day 
        Get
            Return MonthCalendarControl.FirstDayOfWeek
        End Get
        Set
            value = MonthCalendarControl.FirstDayOfWeek
        End Set
    End Property

    ' Expose the AddBoldedDate method.
    Public Sub AddBoldedDate(ByVal dateToBold As DateTime) 
        MonthCalendarControl.AddBoldedDate(dateToBold)

    End Sub

    ' Subscribe and unsubscribe the control events you wish to expose.
    Protected Overrides Sub OnSubscribeControlEvents(ByVal c As Control) 

        ' Call the base so the base events are connected.
        MyBase.OnSubscribeControlEvents(c)

        ' Cast the control to a MonthCalendar control.
        Dim monthCalendarControl As MonthCalendar = _
            CType(c, MonthCalendar)

        ' Add the event.
        AddHandler monthCalendarControl.DateChanged, _
            AddressOf HandleDateChanged

    End Sub

    Protected Overrides Sub OnUnsubscribeControlEvents(ByVal c As Control)
        ' Call the base method so the basic events are unsubscribed.
        MyBase.OnUnsubscribeControlEvents(c)

        ' Cast the control to a MonthCalendar control.
        Dim monthCalendarControl As MonthCalendar = _
            CType(c, MonthCalendar)

        ' Remove the event.
        RemoveHandler monthCalendarControl.DateChanged, _
            AddressOf HandleDateChanged

    End Sub

    ' Declare the DateChanged event.
    Public Event DateChanged As DateRangeEventHandler

    ' Raise the DateChanged event.
    Private Sub HandleDateChanged(ByVal sender As Object, _
        ByVal e As DateRangeEventArgs)

        RaiseEvent DateChanged(Me, e)
    End Sub
End Class
 //Declare a class that inherits from ToolStripControlHost.
    public class ToolStripMonthCalendar : ToolStripControlHost
    {
        // Call the base constructor passing in a MonthCalendar instance.
        public ToolStripMonthCalendar() : base (new MonthCalendar()) { }

        public MonthCalendar MonthCalendarControl
        {
            get
            {
                return Control as MonthCalendar;
            }
        }

        // Expose the MonthCalendar.FirstDayOfWeek as a property.
        public Day FirstDayOfWeek
        {
            get
            {
                return MonthCalendarControl.FirstDayOfWeek;
            }
            set { value = MonthCalendarControl.FirstDayOfWeek; }
        }

        // Expose the AddBoldedDate method.
        public void AddBoldedDate(DateTime dateToBold)
        {
            MonthCalendarControl.AddBoldedDate(dateToBold);
        }

        // Subscribe and unsubscribe the control events you wish to expose.
        protected override void OnSubscribeControlEvents(Control c)
        {
            // Call the base so the base events are connected.
            base.OnSubscribeControlEvents(c);

            // Cast the control to a MonthCalendar control.
            MonthCalendar monthCalendarControl = (MonthCalendar) c;

            // Add the event.
            monthCalendarControl.DateChanged +=
                new DateRangeEventHandler(OnDateChanged);
        }

        protected override void OnUnsubscribeControlEvents(Control c)
        {
            // Call the base method so the basic events are unsubscribed.
            base.OnUnsubscribeControlEvents(c);

            // Cast the control to a MonthCalendar control.
            MonthCalendar monthCalendarControl = (MonthCalendar) c;

            // Remove the event.
            monthCalendarControl.DateChanged -=
                new DateRangeEventHandler(OnDateChanged);
        }

        // Declare the DateChanged event.
        public event DateRangeEventHandler DateChanged;

        // Raise the DateChanged event.
        private void OnDateChanged(object sender, DateRangeEventArgs e)
        {
            if (DateChanged != null)
            {
                DateChanged(this, e);
            }
        }
    }

Compilar el código

  • Este ejemplo requiere:

  • Referencias a los ensamblados System y System.Windows.Forms.

Para obtener información sobre la generación de este ejemplo desde la línea de comandos de Visual Basic o Visual C#, vea Generar desde la línea de comandos (Visual Basic) o Generar la línea de comandos con csc.exe. También puede generar este ejemplo en Visual Studio pegando el código en un proyecto nuevo.

Vea también

Conceptos

Arquitectura del control ToolStrip

Resumen de la tecnología ToolStrip

Referencia

ToolStripControlHost

Información sobre el control ToolStrip (formularios Windows Forms)