Programming C++Builder How to Add Images to a String List in C++Builder
Notice: Undefined variable: relatedArticlesCount in /home/wwwroot/codeback.net/plugins/content/tags.php on line 149

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:

void __fastcallTFMForm::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
  }
}

Note: To run this sample codes, you need to include "dir.h" in your header, like this:

#include
#pragma hdrstop

#include "Unit1.h"
#include "dir.h"    //This is the line you need to add in your program.
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"


Add comment


Security code
Refresh

Programming - C++Builder