Gewusst wie: Bereitstellen einer Toolboxbitmap für ein Steuerelement

Wenn in der Toolbox ein spezielles Symbol für das Steuerelement angezeigt werden soll, können Sie mithilfe des ToolboxBitmapAttribute ein Bild festlegen. Diese Klasse ist ein Attribut, d. h. eine besondere Art von Klasse, die an andere Klassen angefügt werden kann. Weitere Informationen über Attribute finden Sie unter Übersicht über Attribute in Visual Basic für Visual Basic und Attribute (C#-Programmierhandbuch) für Visual C#.

Mithilfe von ToolboxBitmapAttribute können Sie eine Zeichenfolge angeben, die den Pfad und den Dateinamen für eine Bitmap von 16 x 16 Pixel angibt. Diese Bitmap wird, nachdem sie der Toolbox hinzugefügt wurde, neben dem Steuerelement angezeigt. Sie können auch einen Type angeben, sodass die diesem Typ zugeordnete Bitmap geladen wird. Wenn Sie sowohl Type als auch eine Zeichenfolge angeben, sucht das Steuerelement in der Assembly, die den vom Type-Parameter angegebenen Typ enthält, nach einer Bildressource mit dem vom Zeichenfolgenparameter angegebenen Namen.

So geben Sie eine Toolboxbitmap für das Steuerelement an

  • Fügen Sie der Klassendeklaration des Steuerelements vor dem Schlüsselwort Class für Visual Basic und oberhalb der Klassendeklaration für Visual C# das ToolboxBitmapAttribute hinzu.

    ' 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
    {
    }
    
    // Specifies the bitmap associated with the Button type.
    /** @attribute ToolboxBitmap(Button.class)  */
    class MyControl1 extends UserControl
    {
    }
    // Specifies a bitmap file.
    /** @attribute ToolboxBitmap("C:\\Documents and Settings\\Joe\\MyPics\\myImage.bmp")*/
    class MyControl2 extends UserControl
    {
    }
    // Specifies a type that indicates the assembly to search, and the name 
    // of an image resource to look for.
    /* @attribute ToolboxBitmap(MyControl.class, "MyControlBitmap") */
    class MyControl extends UserControl
    {
    }
    

Siehe auch

Referenz

ToolboxBitmapAttribute

Konzepte

Attribute (C#-Programmierhandbuch)

Weitere Ressourcen

Entwickeln von Windows Forms-Steuerelementen zur Entwurfszeit
Visual Basic-Attribute