RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TOpenDialog.FilterIndex Property

Determines which filter is selected by default when the dialog opens.

Pascal
property FilterIndex: Integer;
C++
__property int FilterIndex;

FilterIndex determines which of the file types in Filter is selected by default when the dialog opens. Set FilterIndex to 1 to choose the first file type in the list as the default, or set FilterIndex to 2 to choose the second file type as the default, and so forth. If the value of FilterIndex is out or range, the first file type listed in Filter is the default.  

C++ Examples: 

 

/*
This example uses an Open dialog box, a memo, and a button
on a form. When the user clicks the button, the Open dialog
box appears. When the user selects files in the dialog box
and chooses the OK button, the first line from each of the
files is added to the memo. Choose multiple files using the
CNTL key or the SHIFT key.
*/

#include <stdio.h>     // for FILE

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  FILE *stream;
  char FirstLine[512];
  
  OpenDialog1->Options.Clear();
  OpenDialog1->Options << ofAllowMultiSelect << ofFileMustExist;
  OpenDialog1->Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
  OpenDialog1->FilterIndex = 2; // start the dialog showing all files 
  if (OpenDialog1->Execute())
  {
    for (int I = 0; I < OpenDialog1->Files->Count; I ++)
    {
      stream = fopen(AnsiString(OpenDialog1->Files->Strings[I]).c_str(), "r");
      if (stream)
      {
        // read the first line from the file
        fgets(FirstLine, sizeof(FirstLine), stream);
        Memo1->Lines->Append(FirstLine);
        fclose(stream);
      }
    }
  }
}

 

Delphi Examples: 

{
This example uses an Open dialog box, a memo, and a button
on a form. When the user clicks the button, the Open dialog
box appears. When the user selects files in the dialog box
and chooses the OK button, the first line from each of the
files is added to the memo. Choose multiple files using the
CNTL key or the SHIFT key.
}
procedure TForm1.Button1Click(Sender: TObject);
var
  I: integer;
  F: TextFile;
  FirstLine: string;
begin
  OpenDialog1.Options := [ofAllowMultiSelect, ofFileMustExist];
  OpenDialog1.Filter := 'Text files (*.txt)|*.txt|All files (*.*)|*.*';
  OpenDialog1.FilterIndex := 2; { start the dialog showing all files } 
  if OpenDialog1.Execute then
    with OpenDialog1.Files do
      for I := 0 to Count - 1 do
      begin
        AssignFile(F, Strings[I]);  { next file in Files property }
        Reset(F);
        Readln(F, FirstLine);  { Read the first line out of the file }
        Memo1.Lines.Append(FirstLine);  { Add the line to the memo }
        CloseFile(F);
      end;
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!