다음을 통해 공유


방법: ToolStripControlHost를 사용하여 Windows Forms 컨트롤 래핑

업데이트: 2007년 11월

ToolStripControlHostToolStripControlHost 생성자를 사용하거나 ToolStripControlHost 자체를 확장하여 임의의 Windows Forms 컨트롤을 호스팅할 수 있도록 디자인되었습니다. ToolStripControlHost를 확장하고 컨트롤에서 자주 사용되는 속성과 메서드를 노출하는 속성과 메서드를 구현하여 컨트롤을 쉽게 래핑할 수 있습니다. ToolStripControlHost 수준의 컨트롤에 대한 이벤트를 노출할 수도 있습니다.

파생을 통해 ToolStripControlHost에서 컨트롤을 호스팅하려면

  1. ToolStripControlHost를 확장합니다. 원하는 컨트롤에 전달하는 기본 클래스 생성자를 호출하는 기본 생성자를 구현합니다.

    ' 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. 래핑되는 컨트롤과 동일한 형식의 속성을 선언하고 속성의 접근자에 올바른 컨트롤 형식으로 Control을 반환합니다.

    Public ReadOnly Property MonthCalendarControl() As MonthCalendar 
        Get
            Return CType(Control, MonthCalendar)
        End Get
    End Property
    
         public MonthCalendar MonthCalendarControl
            {
                get
                {
                    return Control as MonthCalendar;
                }
            }
    
  3. 확장된 클래스의 속성과 메서드를 사용하여 래핑된 컨트롤에서 자주 사용되는 그 밖의 속성과 메서드를 노출합니다.

    ' 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. 원하는 경우 OnSubscribeControlEventsOnUnsubscribeControlEvents 메서드를 재정의하고 노출할 컨트롤 이벤트를 추가합니다.

    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. 노출할 이벤트에 필요한 래핑을 제공합니다.

        ' 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);
                }
            }
    

예제

'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);
            }
        }
    }

코드 컴파일

  • 이 예제에는 다음 사항이 필요합니다.

  • System 및 System.Windows.Forms 어셈블리에 대한 참조

Visual Basic 또는 Visual C#의 명령줄에서 이 예제를 빌드하는 방법에 대한 자세한 내용은 명령줄에서 빌드(Visual Basic) 또는 csc.exe를 사용한 명령줄 빌드를 참조하십시오. Visual Studio에서 코드를 새 프로젝트에 붙여넣어 이 예제를 빌드할 수도 있습니다.

참고 항목

개념

ToolStrip 컨트롤 아키텍처

ToolStrip 기술 요약

참조

ToolStripControlHost

ToolStrip 컨트롤 개요(Windows Forms)