These procedures add 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 ComboBox component
- Choose FileNewOtherDelphi for .NET ProjectsVCL Forms Application. The VCL Forms Designer displays.
- Click the Design tab to display the form.
- From the Win32 category of the Tool Palette, place an TImageList component on the form.
- From the Standard category of the Tool Palette, place a TComboBox component on the form.
To set the component properties
- Select ComboBox1 on the form.
- In the Object Inspector, set the Style property drop-down to csOwnerDrawFixed.
- In the Object Inspector, click the [...] 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 and then click OK.
- Double-click ImageList1 in the form. The ImageList Editor displays.
- Click the Add button to display the Add Images dialog.
- Browse your local drive to locate a bitmap image to display in the combobox.
- 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 Designer, select ComboBox1.
- In the Object Inspector, click the Events tab.
- Double-click the OnDrawItem event. The Code Editor displays with cursor in the code block of the DrawItem 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]);
To run the program
- Save all files in your project.
- Choose RunRun. The application executes, displaying a form with a combo box.
- Click the combobox drop-down. The bitmap image and the text string display as a list item.