Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get the extension of the filename to change when the filetype is changed in a save dialog box

How To

Get the extension of the filename to change when the filetype is changed in a save dialog box

by  DelphiAaron  Posted    (Edited  )
Many products have this feature on their save dialogs, e.g. in Excel if the filename in the Save dialog is showing as "Book1.xls" and you change the File Type to ".txt" then filename is automatically changed to "Book1.txt"

This example uses an standard FileSaveAs Action with an embedded dialog.

First add a reference to CommDlg.
Code:
Uses 
 CommDlg;
This code is attached to the change file type event which fires when the user changes the extension combobox.

Then extract the new extension from the filter list, change the filename and send it back to the dialog box.
Code:
procedure TForm1.FileSaveAs1SaveDialogTypeChange(Sender: TObject); 
var 
   I : Integer; 
   Fn, Ext : String; 
   ExtList : TStringList; 
const 
     CB_FILENAME_ID = 1148; 
begin 
     ExtList:=TStringList.Create; 
     try 
        ExtList.Delimiter:='|'; 
        ExtList.DelimitedText:=FileSaveAs1.Dialog.Filter; 
        Ext:=ExtList.Strings[FileSaveAs1.Dialog.FilterIndex*2-1]; 
     finally 
        ExtList.Free; 
     end; 
     I:=Pos(Ext,';'); 
     if I>0 then Ext:=Copy(Ext,1,I-1); 
     Ext:=StringReplace(Ext,'*','',[rfReplaceAll]); 
     Fn:=ChangeFileExt(ExtractFilename(FileSaveAs1.Dialog.FileName),Ext); 
     SendMessage( GetParent(FileSaveAs1.Dialog.Handle), CDM_SETCONTROLTEXT, CB_FILENAME_ID, LongInt(Pchar(Fn))); 
end;
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top