System.Windows.Forms


.NET Framework クラス ライブラリ
ToolStripControlHost クラス

メモ : このクラスは、.NET Framework version 2.0 で新しく追加されたものです。

カスタム コントロールまたは Windows フォーム コントロールをホストします。

名前空間: System.Windows.Forms
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)

構文

Visual Basic (宣言)
Public Class ToolStripControlHost
    Inherits ToolStripItem
Visual Basic (使用法)
Dim instance As ToolStripControlHost
C#
public class ToolStripControlHost : ToolStripItem
C++
public ref class ToolStripControlHost : public ToolStripItem
J#
public class ToolStripControlHost extends ToolStripItem
JScript
public class ToolStripControlHost extends ToolStripItem
解説

ToolStripControlHost クラスを使用すると、カスタマイズしたコントロール、またはその他の Windows フォーム コントロールをホストできます。

ToolStripItem をカスタマイズするには、ToolStripControlHost からクラスを派生させて、カスタムの実装を作成します。OnSubscribeControlEvents などのメソッドをオーバーライドすることにより、ホストされるコントロールが発生させるイベントを処理できます。また、カスタムの機能をプロパティに追加することにより、ホストされるコントロールを拡張できます。

使用例

MonthCalendar コントロールを持つ ToolStripControlHost を構築し、OnSubscribeControlEvents を使用してイベントを処理し、メンバの一部を ToolStripControlHost に公開するコード例を次に示します。

Visual Basic
'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
C#
//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.Object
   System.MarshalByRefObject
     System.ComponentModel.Component
       System.Windows.Forms.ToolStripItem
        System.Windows.Forms.ToolStripControlHost
           System.Windows.Forms.ToolStripComboBox
           System.Windows.Forms.ToolStripProgressBar
           System.Windows.Forms.ToolStripTextBox
スレッド セーフ

この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。
プラットフォーム

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。

バージョン情報

.NET Framework

サポート対象 : 2.0
参照

タグ :


Page view tracker