RAD Studio for Microsoft .NET
ContentsIndex
PreviousUpNext
Placing a Bitmap Image in a Control in a VCL Forms Application

These procedures add a bitmap image to a combo box in a VCL forms application.

  1. Create a VCL form.
  2. Place components on the form.
  3. Set component properties in the Object Inspector.
  4. Write event handlers for the component's drawing action.
  5. Build and run the application.

To create a VCL form with a ComboBox component

  1. Choose FileNewOtherDelphi for .NET ProjectsVCL Forms Application. The VCL Forms Designer displays.
  2. Click the Design tab to display the form.
  3. From the Win32 category of the Tool Palette, place an TImageList component on the form.
  4. From the Standard category of the Tool Palette, place a TComboBox component on the form.

To set the component properties

  1. Select ComboBox1 on the form.
  2. In the Object Inspector, set the Style property drop-down to csOwnerDrawFixed.
  3. In the Object Inspector, click the [...] next to the Items property. The String List Editor displays.
  4. Enter a string you would like to associate with the bitmap image, for example, MyImage and then click OK.
  5. Double-click ImageList1 in the form. The ImageList Editor displays.
  6. Click the Add button to display the Add Images dialog.
  7. Browse your local drive to locate a bitmap image to display in the combobox.
  8. Select a very small image such as an icon. Copy it to your project directory, and click Open. The image displays in the ImageList Editor.
  9. Click OK to close the editor.

To add the event handler code

  1. In the Designer, select ComboBox1.
  2. In the Object Inspector, click the Events tab.
  3. Double-click the OnDrawItem event. The Code Editor displays with cursor in the code block of the DrawItem event handler.
  4. Enter the following code for the event handler:

ComboBox1.Canvas.FillRect(rect);
ImageList1.Draw(ComboBox1.Canvas, Rect.Left, Rect.Top, Index);
ComboBox1.Canvas.TextOut(Rect.Left+ImageList1.Width+2,
  Rect.Top, ComboBox1.Items[Index]);
To run the program

  1. Save all files in your project.
  2. Choose RunRun. The application executes, displaying a form with a combo box.
  3. Click the combobox drop-down. The bitmap image and the text string display as a list item.

Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!