Storing Pictures in an Image List

   

Many of the controls I'll be discussing in this hour support the capability to attach pictures to different types of items. For instance, the Tree View control, which is used in Explorer for navigating folders, displays images next to each folder node. Not all these pictures are the same; the control uses specific pictures to denote information about each node. It would have been possible for Microsoft to make each control store its images internally, but that would be highly inefficient because it wouldn't allow controls to share the same pictures; you'd have to store the pictures in each control that needed them. Instead, Microsoft created a control dedicated to storing pictures and serving them to other controls: the Image List.

Create a new Windows Application named Lists and Trees. Change the name of the default form to fclsListsAndTrees, set its Text property to Lists and Trees Example, and set the entry point of the project to reference fclsListsAndTrees instead of Form1. Next, add a new Image List control by double-clicking the ImageList item in the toolbox. Like the timer, the Image List is an invisible-at-runtime control, so it appears below the form. Change the name of the Image List to imgMyImages.

The sole purpose of an Image List control is to store pictures and make them available to other controls. The pictures of an Image List are stored in the Images collection of the control. Click the Images property of the control in the Properties window and then click the small button that appears. C# then displays the Image Collection Editor, which is similar to other editors you've used in this hour. Click Add to display the Open dialog box and use this dialog box to locate and select a 16x16 pixel bitmap. If you don't have a 16x16 pixel bitmap, you can create one using Microsoft Paint, or you can download samples I've provided at http://www.samspublishing.com/detail_sams.cfm?item=0672322870. After you've added an image, click OK to close the Image Collection Editor.

Take a look at the ImageSize property of the Image control. It should read 16,16. If it doesn't, the bitmap you selected is not 16x16 pixels; this property sets itself to the dimensions of the first picture added to the Image List.

You can't always rely on the background where a picture will be displayed to be white ”or any other color for that matter. Because of this, the Image List has a TransparentColor property. By default, this is set to Transparent, which essentially means that no color in the picture is transparent (if the pictures in the Image List are icons, rather than bitmaps, the transparent portion of the icons will remain transparent). If you designate a specific color for the TransparentColor property, when a picture is served from the Image List to another control, all occurrences of the specified color will appear transparent ”the background will show through. This gives you the power to create pictures that can be served to controls without concern about the color on which the picture will appear.

That's all there is to adding images to an Image List. The power of the Image List resides in its capability to be linked to by other controls that can access the pictures it stores.


   
Top


Sams Teach Yourself C# in 24 Hours
Sams Teach Yourself Visual Basic 2010 in 24 Hours Complete Starter Kit (Sams Teach Yourself -- Hours)
ISBN: 0672331136
EAN: 2147483647
Year: 2002
Pages: 253
Authors: James Foxall

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net