RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TCustomListBox.Selected Property

Indicates whether a particular item is selected.

Pascal
property Selected [Index: Integer]: Boolean;
C++
__property Boolean Selected[int Index];

Use Selected to query the state of a particular item in the list box. If the item specified by the Index parameter is selected in the list box, the value of the Selected property is true. If the specified item is not selected, Selected is false. Set Selected to change the currently selected item. 

The Index parameter is the item referenced by its position in the list box, with the first item having an Index value of 0. 

An item is selected when the user highlights it. More than one item in the list box can be selected by setting the MultiSelect property to true. In this case, the Selected item is the one that has focus when the list box has input focus.  

C++ Examples: 

 

/*
This example uses a file listbox and a regular listbox on a
form. The following routine scans through the files listed
in the file listbox and lists the sizes of any selected
files to the regular list box.  To exercise the error
condition create a file in the Debug directory, start this
application and then delete the file.  Now try to list the
size of the deleted file.  Continue will continue listing
file sizes in a multiple selection that includes the deleted
file.  Break will stop.  Set the MultiSelect and
ExtendedSelect properties on the FileListBox.
*/
#include <stdio.h>     // for FILE, fopen, fstat, fileno, fread and fclose
#include <sys\stat.h>  // for fstat and the stat struct

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  for (int i = 0; i < FileListBox1->Items->Count; i++)
  {
    if (FileListBox1->Selected[i])
    {
      if (!FileExists(FileListBox1->Items->Strings[i]))
      {
        MessageBeep(0);
        if (Application->MessageBox(
          AnsiString("File " + FileListBox1->Items->Strings[i] + "not found").c_str(),
          NULL,
          MB_OKCANCEL) == IDCANCEL)
          break;
        else
          continue;
      }
      FILE *F = fopen(FileListBox1->Items->Strings[i].c_str(),"r");
      struct stat statbuf;
      fstat(fileno(F), &statbuf);
      ListBox1->Items->Add(
          FileListBox1->Items->Strings[i] + ": " + IntToStr(statbuf.st_size));
      fclose(F);
    }
  }
}

 

Delphi Examples: 

{
This example uses a file listbox and a regular listbox on a
form. The following routine scans through the files listed
in the file listbox and lists the sizes of any selected
files to the regular list box.  To exercise the error
condition create a file in the Debug directory, start this
application and then delete the file.  Now try to list the
size of the deleted file.  Set the MultiSelect and
ExtendedSelect properties on the FileListBox.
}
procedure TForm1.Button1Click(Sender: TObject);
var
  F: File;
  i, filehandle: Integer;
begin
  for i := 0 to (FileListBox1.Items.Count - 1) do begin
  try
    if FileListBox1.Selected[i] then 
    begin
      if not FileExists(FileListBox1.Items.Strings[i]) then
      begin
        MessageDlg('File: ' + FileListBox1.Items.Strings[i] +
                   ' not found', mtError, [mbOk], 0);
        Continue;
      end;
      filehandle:=  FileOpen(FileListBox1.Items.Strings[i], 1);
      if (filehandle = -1) then
      begin
        MessageDlg('File: ' + FileListBox1.Items.Strings[i] +
                   ' cannot be opened with access mode 1.', mtError, [mbOk], 0);
        Continue;
      end
      else
        FileClose(filehandle);

      AssignFile(F, FileListBox1.Items.Strings[i]);
      Reset(F, 1);
      ListBox1.Items.Add(
        FileListBox1.Items.Strings[i] + ': ' + IntToStr(FileSize(F)));
      CloseFile(F);
    end;
   finally
   { do something here }
   end;
  end;
end;

 

extendedselect 

Items 

MultiSelect 

SelCount

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