This procedure adds a bitmap image to a combo box in a VCL forms application.
- Create a VCL form.
- Place components on the form.
- Set component properties in the Object Inspector.
- Write event handlers for the component's drawing action.
- Build and run the application.
To create a VCL form with a TComboBox component
- Choose FileNewOtherDelphi Projects or C++Builder Projects and double-click the VCL Forms Application icon. The VCL Forms Designer is displayed.
- From the Win32 page of the Tool Palette, place an TImageList component on the form.
- From the Standard page of the Tool Palette, place a TComboBox on the form.
To set the component properties
- Select ComboBox1 in the form.
- In the Object Inspector, set the Style property drop-down to csOwnerDrawFixed.
- In the Object Inspector, click the ellipsis next to the Items property. The String List Editor displays.
- Enter a string you would like to associate with the bitmap image, for example, MyImage; then click OK.
- Double-click ImageList1 in the form. The ImageList editor displays.
- Click the Add button to display the Add Images dialog.
- Locate a bitmap image to display in the Combo box. To locate an image, you can search for *.bmp images on your local drive. 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.
- Click OK to close the editor.
To add the event handler code
- In the VCL form view, select ComboBox1.
- In the Object Inspector, click the Events page, and double-click the OnDrawItem event. The Code Editor displays with cursor in the code block of the ComboBox1DrawItem (Delphi) or ComboBox1::DrawItem (C++) event handler.
- 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]);
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
- Choose RunRun. The applications executes, displaying a form with a combo box.
- Click the combo box drop-down. The bitmap image and the text string display as a list item.