--- title: "How to: Define an Icon for a ToolBar Button" ms.date: "03/30/2017" dev_langs: - "csharp" - "vb" - "cpp" helpviewer_keywords: - "toolbars [Windows Forms], adding icons to buttons" - "buttons [Windows Forms], icons" - "examples [Windows Forms], toolbars" - "images [Windows Forms], toolbar buttons" - "icons [Windows Forms], toolbar buttons" - "ToolBar control [Windows Forms], adding icons to buttons" ms.assetid: 84db98b4-8566-49ce-b2c8-1fd66a5eb3a0 --- # How to: Define an Icon for a ToolBar Button > [!NOTE] > The control replaces and adds functionality to the control; however, the control is retained for both backward compatibility and future use, if you choose. buttons are able to display icons within them for easy identification by users. This is achieved through adding images to the [ImageList Component](imagelist-component-windows-forms.md) component and then associating the component with the control. ### To set an icon for a toolbar button programmatically 1. In a procedure, instantiate an component and a control. 2. In the same procedure, assign an image to the component. 3. In the same procedure, assign the control to the control and assign the property of the individual toolbar buttons. In the following code example, the path set for the location of the image is the **My Documents** folder. This is done, because you can assume that most computers running the Windows operating system will include this directory. This also allows users with minimal system access levels to safely run the application. The example below assumes a form with a control already added. Following the steps above, you should have written code similar to that displayed below. ```vb 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. ' You should replace the bold image ' in the sample below with an icon of your own choosing. Dim myImage As System.Drawing.Image = _ Image.FromFile Image.FromFile _ (System.Environment.GetFolderPath _ (System.Environment.SpecialFolder.Personal) _ & "\Image.gif") ImageList1.Images.Add(myImage) ' Create a ToolBarButton. 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 ``` ```csharp public void InitializeMyToolBar() { // Instantiate an ImageList component and a ToolBar control. ToolBar toolBar1 = new ToolBar(); ImageList imageList1 = new ImageList(); // Assign an image to the ImageList component. // You should replace the bold image // in the sample below with an icon of your own choosing. // Note the escape character used (@) when specifying the path. Image myImage = Image.FromFile (System.Environment.GetFolderPath (System.Environment.SpecialFolder.Personal) + @"\Image.gif"); imageList1.Images.Add(myImage); // Create a ToolBarButton. ToolBarButton toolBarButton1 = new ToolBarButton(); // Add the ToolBarButton to the ToolBar. toolBar1.Buttons.Add(toolBarButton1); // Assign an ImageList to the ToolBar. toolBar1.ImageList = imageList1; // Assign ImageIndex property of the ToolBarButton. toolBarButton1.ImageIndex = 0; } ``` ```cpp public: void InitializeMyToolBar() { // Instantiate an ImageList component and a ToolBar control. ToolBar ^ toolBar1 = gcnew ToolBar(); ImageList ^ imageList1 = gcnew ImageList(); // Assign an image to the ImageList component. // You should replace the bold image // in the sample below with an icon of your own choosing. Image ^ myImage = Image::FromFile(String::Concat (System::Environment::GetFolderPath (System::Environment::SpecialFolder::Personal), "\\Image.gif")); imageList1->Images->Add(myImage); // Create a ToolBarButton. ToolBarButton ^ toolBarButton1 = gcnew ToolBarButton(); // Add the ToolBarButton to the ToolBar. toolBar1->Buttons->Add(toolBarButton1); // Assign an ImageList to the ToolBar. toolBar1->ImageList = imageList1; // Assign ImageIndex property of the ToolBarButton. toolBarButton1->ImageIndex = 0; } ``` ## See also - - [How to: Trigger Menu Events for Toolbar Buttons](how-to-trigger-menu-events-for-toolbar-buttons.md) - [ToolBar Control](toolbar-control-windows-forms.md) - [ImageList Component](imagelist-component-windows-forms.md)