مشاركة عبر


كيفية القيام بما يلي: التفاف عنصر تحكم Windows Forms مع ToolStripControlHost

ToolStripControlHostهو تم تصميمها لتمكين استضافة عناصر تحكم النماذج Windows إجبارية باستخدامToolStripControlHostالمنشئ أو بتوسيعToolStripControlHostنفسه. من السهل للالتفاف عنصر تحكم بتوسيع ToolStripControlHostوتطبيق الخصائص والأساليب التي تكشف بشكل متكرر باستخدام الخصائص والأساليب الخاصة عنصر تحكم. يمكن أيضا عرض الأحداث عنصر تحكم في ToolStripControlHostالمستوى.

إلى استضافة عنصر تحكم في olStripControlHost إلى بواسطة الاشتقاق

  1. قم بتوسيع ToolStripControlHost. تطبيق الدالة الإنشائية افتراضي الذي يستدعي الدالة الإنشائية فئة Base تمرير في المطلوب عنصر تحكم.

    ' 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()) { }
    
    // Call the base constructor passing in a MonthCalendar instance.
    ToolStripMonthCalendar() : ToolStripControlHost( gcnew 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;
                }
            }
    
    property MonthCalendar^ MonthCalendarControl 
    {
       MonthCalendar^ get()
       {
          return static_cast<MonthCalendar^>(Control);
       }
    }
    
  3. expose الأخرى المستخدمة بكثرة الخصائص والأساليب الخاصة الملتفة عنصر تحكم مع الخصائص والأساليب في فئة موسعة.

    ' 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);
            }
    
    property Day FirstDayOfWeek 
    {
       // Expose the MonthCalendar.FirstDayOfWeek as a property.
       Day get()
       {
          return MonthCalendarControl->FirstDayOfWeek;
       }
    
       void set( Day value )
       {
          value = MonthCalendarControl->FirstDayOfWeek;
       }
    }
    
    // Expose the AddBoldedDate method.
    void AddBoldedDate( DateTime dateToBold )
    {
       MonthCalendarControl->AddBoldedDate( dateToBold );
    }
    
  4. بشكل اختياري، يمنع OnSubscribeControlEvents، و OnUnsubscribeControlEventsوظائف وقم بإضافة عنصر تحكم الأحداث التي تريد أن 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
    
            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);
            }
    
    void OnSubscribeControlEvents( System::Windows::Forms::Control^ c )
    {
       // Call the base so the base events are connected.
       __super::OnSubscribeControlEvents( c );
    
       // Cast the control to a MonthCalendar control.
       MonthCalendar^ monthCalendarControl = (MonthCalendar^)c;
    
       // Add the event.
       monthCalendarControl->DateChanged += gcnew DateRangeEventHandler( this, &ToolStripMonthCalendar::HandleDateChanged );
    }
    
    void OnUnsubscribeControlEvents( System::Windows::Forms::Control^ c )
    {
    
       // Call the base method so the basic events are unsubscribed.
       __super::OnUnsubscribeControlEvents( c );
    
       // Cast the control to a MonthCalendar control.
       MonthCalendar^ monthCalendarControl = (MonthCalendar^)c;
    
       // Remove the event.
       monthCalendarControl->DateChanged -= gcnew DateRangeEventHandler( this, &ToolStripMonthCalendar::HandleDateChanged );
    }
    
  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 the DateChanged event.
    // Raise the DateChanged event.
    void HandleDateChanged( Object^ sender, DateRangeEventArgs^ e )
    {
       if ( DateChanged != nullptr )
       {
          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);
            }
        }
    }
//Declare a class that inherits from ToolStripControlHost.
public ref class ToolStripMonthCalendar: public ToolStripControlHost
{
public:
   // Call the base constructor passing in a MonthCalendar instance.
   ToolStripMonthCalendar() : ToolStripControlHost( gcnew MonthCalendar ) {}

   property MonthCalendar^ MonthCalendarControl 
   {
      MonthCalendar^ get()
      {
         return static_cast<MonthCalendar^>(Control);
      }
   }
   property Day FirstDayOfWeek 
   {
      // Expose the MonthCalendar.FirstDayOfWeek as a property.
      Day get()
      {
         return MonthCalendarControl->FirstDayOfWeek;
      }

      void set( Day value )
      {
         value = MonthCalendarControl->FirstDayOfWeek;
      }
   }

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

protected:
   // Subscribe and unsubscribe the control events you wish to expose.
   void OnSubscribeControlEvents( System::Windows::Forms::Control^ c )
   {
      // Call the base so the base events are connected.
      __super::OnSubscribeControlEvents( c );

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

      // Add the event.
      monthCalendarControl->DateChanged += gcnew DateRangeEventHandler( this, &ToolStripMonthCalendar::HandleDateChanged );
   }

   void OnUnsubscribeControlEvents( System::Windows::Forms::Control^ c )
   {

      // Call the base method so the basic events are unsubscribed.
      __super::OnUnsubscribeControlEvents( c );

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

      // Remove the event.
      monthCalendarControl->DateChanged -= gcnew DateRangeEventHandler( this, &ToolStripMonthCalendar::HandleDateChanged );
   }

public:
   event DateRangeEventHandler^ DateChanged;

private:
   // Declare the DateChanged event.
   // Raise the DateChanged event.
   void HandleDateChanged( Object^ sender, DateRangeEventArgs^ e )
   {
      if ( DateChanged != nullptr )
      {
         DateChanged( this, e );
      }
   }
};

التحويل البرمجي للتعليمات البرمجية

  • يتطلب هذا المثال:

  • مراجع عن تجميعات النظام و تجميعات System.Windows.Forms.

للحصول على المزيد من المعلومات حول إنشاء هذا المثال من خط أوامر Visual Basic أو #Visual C، راجع الإنشاء من سطر الأوامر (Visual Basic) أو إنشاء سطر الأوامر باستخدام csc.exe. يمكنك أيضاً بناء هذا المثال في Visual Studio عن طريق لصق التعليمات البرمجية في مشروع جديد. لمزيد من المعلومات، راجع: كيفية القيام بما يلي: ترجمة و تشغيل مثال التعليمات برمجية لنماذج Windows الكامل باستخدام ‏‫Visual Studio.

راجع أيضًا:

المرجع

ToolStripControlHost

نظرة عامة عن عنصر تحكم (ToolStrip (Windows Forms

المبادئ

هندسة عنصر تحكم ToolStrip

خلاصة سلسلة الأدوات التقنية