Procedure: Een pictogram voor een knop op de werkbalk definiëren
Notitie
Het ToolStrip besturingselement vervangt en voegt functionaliteit toe aan het ToolBar besturingselement; de ToolBar controle wordt echter behouden voor zowel achterwaartse compatibiliteit als toekomstig gebruik, indien u kiest.
ToolBar knoppen kunnen pictogrammen weergeven voor eenvoudige identificatie door gebruikers. Dit wordt bereikt door afbeeldingen toe te voegen aan de ImageList Component component en vervolgens het ImageList component te koppelen aan het ToolBar besturingselement.
Een pictogram voor een werkbalkknop programmatisch instellen
Maak in een procedure een instantie van een ImageList onderdeel en een ToolBar besturingselement.
Wijs in dezelfde procedure een afbeelding toe aan de ImageList component.
Wijs in dezelfde procedure het besturingselement ImageList toe aan het besturingselement ToolBar en wijs de eigenschap ImageIndex van de afzonderlijke werkbalkknoppen toe.
In het volgende codevoorbeeld is het pad dat is ingesteld voor de locatie van de afbeelding de map Mijn documenten. Dit gebeurt omdat u ervan kunt uitgaan dat de meeste computers met het Windows-besturingssysteem deze map bevatten. Hierdoor kunnen gebruikers met minimale systeemtoegangsniveaus de toepassing veilig uitvoeren. In het onderstaande voorbeeld wordt aangenomen dat er al een formulier is toegevoegd met een PictureBox controle.
Als u de bovenstaande stappen volgt, moet u code hebben geschreven die vergelijkbaar is met de code die hieronder wordt weergegeven.
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
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; }
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; }
Zie ook
.NET Desktop feedback