Setting an Icon for a Toolbar Button Programmatically

You set an icon for a toolbar button programmatically by performing the following steps:

1. In a procedure, instantiate an ImageList component and a ToolBar control.

2. In the same procedure, assign an image to the ImageList component.

3. In the same procedure, assign the ImageList control to the ToolBar control and assign the ImageIndex property of the individual toolbar buttons.

The following code is derived from these steps: Public Sub InitializeMyToolBar()

' Instantiate an ImageList component and a ToolBar control.

Dim ToolBar1 as New ToolBar

Dim ImageList1 as New ImageList

' Assign an image to the ImageList component.

' The image used below is for demonstration purposes only.

' Replace it with an image of your own choosing.

Dim myImage As System.Drawing.Image = Image.FromFile( "C:\winnt\Sample.ico"

ImageList1.Images.Add(myImage)

' Create a ToolBarB utton.

Dim ToolBarButton1 As New ToolBarButton()

' Add the ToolBarButton to the ToolBar.

ToolBar1.Buttons.Add(toolBarButton1)

' Assign an ImageList to the ToolBar.

ToolBar1.ImageList = ImageList1

' Assign the ImageIndex property of the ToolBarButton.

ToolBarButton1.ImageIndex = 0

End Sub

Was this article helpful?

0 0

Post a comment