方法: コントロールにツールボックス ビットマップを指定する

コントロールの特殊なアイコンを Visual Studio のツールボックスに表示するには、ToolboxBitmapAttribute を使用して特定の画像を指定します。 このクラスは "属性" であり、他のクラスに追加できる特殊なクラスです。 属性の詳細については、「属性の概要 (Visual Basic)」 (Visual Basic の場合) または「属性 (C#)」 (C# の場合) を参照してください。

ToolboxBitmapAttribute を使用すると、16 x 16 ピクセルのビットマップのパスとファイル名を示す文字列を指定できます。 コントロールをツールボックスに追加すると、このビットマップがコントロールの横に表示されます。 また、Type を指定して、その種類に関連付けられたビットマップを読み込むようにすることもできます。 Type と文字列の両方を指定すると、コントロールにより、Type パラメーターで指定した種類の画像が含まれるアセンブリで、文字列パラメーターで指定した名前の画像リソースが検索されます。

コントロールのツールボックス ビットマップを指定するには

  1. コントロールのクラス宣言に ToolboxBitmapAttribute を追加します。visual Basic の場合は Class キーワードの前に、Visual C# の場合はクラス宣言の上に追加します。

    ' Specifies the bitmap associated with the Button type.
    <ToolboxBitmap(GetType(Button))> Class MyControl1
    ' Specifies a bitmap file.
    End Class
    <ToolboxBitmap("C:\Documents and Settings\Joe\MyPics\myImage.bmp")> _
       Class MyControl2
    End Class
    ' Specifies a type that indicates the assembly to search, and the name
    ' of an image resource to look for.
    <ToolboxBitmap(GetType(MyControl), "MyControlBitmap")> Class MyControl
    End Class
    
    // Specifies the bitmap associated with the Button type.
    [ToolboxBitmap(typeof(Button))]
    class MyControl1 : UserControl
    {
    }
    // Specifies a bitmap file.
    [ToolboxBitmap(@"C:\Documents and Settings\Joe\MyPics\myImage.bmp")]
    class MyControl2 : UserControl
    {
    }
    // Specifies a type that indicates the assembly to search, and the name
    // of an image resource to look for.
    [ToolboxBitmap(typeof(MyControl), "MyControlBitmap")]
    class MyControl : UserControl
    {
    }
    
  2. プロジェクトをリビルドします。

    注意

    ビットマップは、自動生成されたコントロールとコンポーネントのツールボックスには表示されません。 ビットマップを表示するには、[ツールボックス アイテムの選択] ダイアログ ボックスを使用してコントロールを再読み込みします。 詳細については、「チュートリアル : ツールボックスへのカスタム コンポーネントの自動設定」を参照してください。

関連項目