Delphi 7 OnTypeChange not updating

553 views Asked by At

I am using a TOpenDialog to allow the user to open 3 different types of files. When showing the initial directory it lists only the files that match the current extension type chosen which is what I want. However, when the user selects one of the other types the filenames that match that type never show up. If I type *.ext in the filename box then all of the filenames of that extension show. How can I get that result when the user changes the filetype?

1

There are 1 answers

11
Remy Lebeau On

If a wildcard mask has been typed in to the edit box, the dialog only displays files that match that mask, regardless of what file type is selected in the drop-down list. The mask is not updated automatically with a new file extension when the user selects a different file type from the list. You have to do that manually in the OnTypeChange event, eg:

uses
  ..., Dlgs, Commdlg;

procedure TForm1.OpenDialog1TypeChange(Sender: TObject);
const
  Extensions : array[1..4] of PChar = ('', 'ext1', 'ext2', 'ext3');
var
  s : String;
  i : Integer;
begin
  OpenDialog1.DefaultExt := Extensions[OpenDialog1.FilterIndex];
  if OpenDialog1.FilterIndex = 1 then Exit;
  i := SendMessage(GetParent(OpenDialog1.Handle), CDM_GETSPEC, 0, 0);
  if i > 1 then
  begin
    SetLength(s, i-1);
    SendMessage(GetParent(OpenDialog1.Handle), CDM_GETSPEC, i, LPARAM(PChar(s)));
    s := ChangeFileExt(s, '.' + Extensions[OpenDialog1.FilterIndex]);
    SendMessage(GetParent(OpenDialog1.Handle), CDM_SETCONTROLTEXT, edt1, LPARAM(PChar(s)));
  end;
end;