Author: William Gerbert
I have two extensions setup in a TSaveDialog box filter section (.bmp and .gif).
How do I force an extension change in the filename editbox that is displayed when I
change the file type dropdown box? Basically if I have picture.bmp in the filename
edit box and I change the file type to gif, how do I force the edit box to change
to picture.gif.
Answer:
Solve 1:
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.
1 uses
2 CommDlg;
3
4 procedure TForm1.SaveDialog1TypeChange(Sender: TObject);
5 var
6 S: string;
7 H: THandle;
8 begin
9 H := GetParent((Sender as TSaveDialog).Handle);
10 S := (Sender as TSaveDialog).Filename;
11 if DirectoryExists(S) then
12 S := '';
13 if S <> '' then
14 with TSaveDialog(Sender) do
15 case FilterIndex of
16 1: S := ChangeFileExt(S, '.rtf');
17 2: S := ChangeFileExt(S, '.txt');
18 else
19 S := '';
20 end;
21 if S <> '' then
22 SendMessage(H, CDM_SETCONTROLTEXT, edt1, longint(PChar(ExtractFileName(S))));
23 end;
Solve 2:
24 uses
25 commdlg, Dlgs;
26
27 procedure TForm1.SaveDialog1TypeChange(Sender: TObject);
28 var
29 newExt: string;
30 filename: string;
31 filenamebuf: array[0..MAX_PATH] of Char;
32 begin
33 case savedialog1.FilterIndex of
34 1: newExt := '.DB';
35 2: newExt := '.DBF';
36 3: newExt := '.GDB';
37 else
38 exit;
39 end;
40 SendMessage(Windows.GetParent(savedialog1.handle), CDM_GETSPEC, MAX_PATH,
41 lparam(@filenamebuf));
42 filename := ChangeFileExt(filenamebuf, newext);
43 SendMessage(Windows.GetParent(savedialog1.handle), CDM_SETCONTROLTEXT,
44 edt1, integer(Pchar(filename)));
45 end;
|