Adding Images to a String List

From RAD Studio
Jump to: navigation, search

Go Up to Adding Graphical Objects to a String List

Once you have graphical images in an application, you can associate them with the strings in a string list. You can either add the objects at the same time as the strings, or associate objects with existing strings. The preferred method is to add objects and strings at the same time, if all the needed data is available.

The following example shows how you might want to add images to a string list. This is part of a file manager application where, along with a letter for each valid drive, it adds a bitmap indicating each drive's type. The OnCreate event handler looks like this:

 procedure TFMForm.FormCreate(Sender: TObject);
 var
   Drive: Char;
   AddedIndex: Integer;
 begin
   for Drive := 'A' to 'Z' do  { iterate through all possible drives }
   begin
     case GetDriveType(Drive + ':/') of  { positive values mean valid drives }
      DRIVE_REMOVABLE:  { add a tab }
        AddedIndex := DriveTabSet.Tabs.AddObject(Drive, Floppy.Picture.Graphic);
      DRIVE_FIXED:  { add a tab }
        AddedIndex := DriveTabSet.Tabs.AddObject(Drive, Fixed.Picture.Graphic);
      DRIVE_REMOTE:  { add a tab }
        AddedIndex := DriveTabSet.Tabs.AddObject(Drive, Network.Picture.Graphic);
     end;
     if UpCase(Drive) = UpCase(DirectoryOutline.Drive) then  { current drive? }
      DriveTabSet.TabIndex := AddedIndex;  { then make that current tab }
   end;
 end;
 void __fastcall TFMForm::FormCreate(TObject *Sender) {
    int AddedIndex;
    char DriveName[4] = "A:\\";
    for (char Drive = 'A'; Drive <= 'Z'; Drive++) // try all possible drives
    {
 	DriveName[0] = Drive;
 	switch (GetDriveType(DriveName)) {
 	case DRIVE_REMOVABLE: // add a list item
 		DriveName[1] = '\0'; // temporarily make drive letter into string 
 		AddedIndex = DriveList->Items->AddObject(DriveName,
 				Floppy->Picture->Graphic);
 		DriveName[1] = ':' // replace the colon
 				break;
 	case DRIVE_FIXED: // add a list item
 		DriveName[1] = '\0'; // temporarily make drive letter into string
 		AddedIndex = DriveList->Items->AddObject(DriveName,
 				Fixed->Picture->Graphic);
 		DriveName[1] = ':' // replace the colon
 				break;
 	case DRIVE_REMOTE: // add a list item
 		DriveName[1] = '\0'; // temporarily make drive letter into string
 		AddedIndex = DriveList->Items->AddObject(DriveName,
 				Network->Picture->Graphic);
 		DriveName[1] = ':' // replace the colon
 					break;
 	}
 		if ((int)(Drive - 'A') == getdisk()) // current drive?
 					DriveList->ItemIndex = AddedIndex;
 		// then make that the current list item
 	}
 }

See Also