Sams Teach Yourself C# in 24 Hours
- Table of Contents
- Copyright
- About the Authors
- Acknowledgments
- Tell Us What You Think!
- Introduction
- Audience and Organization
- Conventions Used in This Book
- Onward and Upward!
- Part I. The Visual Studio Environment
- Hour 1. A C# Programming Tour
- Hour 2. Navigating C#
- Hour 3. Understanding Objects and Collections
- Hour 4. Understanding Events
- Part II. Building a User Interface
- Hour 5. Building FormsPart I
- Hour 6. Building FormsPart II
- Hour 7. Working with the Traditional Controls
- Hour 8. Advanced Controls
- Hour 9. Adding Menus and Toolbars to Forms
- Hour 10. Drawing and Printing
- Part III. Making Things HappenProgramming!
- Hour 11. Creating and Calling Methods
- Hour 12. Using Constants, Data Types, Variables, and Arrays
- Hour 13. Performing Arithmetic, String Manipulation, and Date/Time Adjustments
- Hour 14. Making Decisions in C# Code
- Hour 15. Looping for Efficiency
- Hour 16. Debugging Your Code
- Hour 17. Designing Objects Using Classes
- Hour 18. Interacting with Users
- Part IV. Working with Data
- Hour 19. Performing File Operations
- Hour 20. Controlling Other Applications Using Automation
- Hour 21. Working with a Database
- Part V. Deploying Solutions and Beyond
- Hour 22. Deploying a Solution
- Hour 23. Introduction to Web Development
- Hour 24. The 10,000-Foot View
- Appendix A. Answers to Quizzes/Exercises
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.
Building Enhanced Lists Using the List View | Next Section

Account Sign In
View your cart