Mirror

Change the file name in a TSaveDialog when the user selects a different file type (Views: 100)


Problem/Question/Abstract:

How to change the file name in a TSaveDialog when the user selects a different file type

Answer:

You could try this. I'm not sure if the line S := (Sender as TSaveDialog).Filename; works in Delphi 5 or earlier but otherwise it should be okay.

uses
  CommDlg;

procedure TForm1.SaveDialog1TypeChange(Sender: TObject);
var
  S: string;
  H: THandle;
begin
  H := GetParent((Sender as TSaveDialog).Handle);
  S := (Sender as TSaveDialog).Filename;
  if DirectoryExists(S) then
    S := '';
  if S <> '' then
    with TSaveDialog(Sender) do
      case FilterIndex of
        1: S := ChangeFileExt(S, '.rtf');
        2: S := ChangeFileExt(S, '.txt');
      else
        S := '';
      end;
  if S <> '' then
    SendMessage(H, CDM_SETCONTROLTEXT, edt1, longint(PChar(ExtractFileName(S))));
end;

<< Back to main page