Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

SaveDialog box and its intelligent implementation

157 views
Skip to first unread message

Alex Siron

unread,
Apr 2, 2008, 11:37:10 AM4/2/08
to
Dear Colleagues,

On my main form, I have (in addition to others components) a TChart1, Memo1,
SaveDialog1 and a button. The Memo1 is a place-holder for some numerical
output of a calculation while the TChart1 does the drawing.

The output of the calculation must either be saved in text or graphic format
by the help of the OnClick event of the button using the intelligence of
SaveDialog box.

In my present implementation/coding (Delphi 5 environment, see the code
below), the user of the program is forced to add the infix of the file
format to determine which kind of file to save, namely
output.txt or myfile.bmp.
If the user just writes, say "output", the program raises an error message.

How can I put some intelligence into the coding, so that the outcome of the
resulting output depends on recently-focused Filter of the SaveDialog1-box.
I have as Filters of the Savedialog1 box the following:
*.txt
*.bmp
*.wmf
*.emf
That is, if the user just writes "output" when the Filter "*.txt" is
focused/active, the resulting file should be: output.txt. Likewise if the
user just writes "output" when the Filter "*.wmf" is focused/active, the
resulting file name should be: output.wmf.

This kind of implementation is incorporated into some programs such as
Microsoft Word, Microsoft Picture It! Photo 7.0; etc.

Please, can anyone provide me with an intelligent example-code how this
issue can be solved.
Many thanks in advance.

With best regards,
Alex Siron from FINLAND

procedure TForm1.SaveAsToolButtonClick(Sender: TObject);
var
F: System.Text;
i: Integer;
S: String;

begin
if SaveDialog1.Execute then
with SaveDialog1 do begin
S:=UpperCase(ExtractFileExt(FileName));
if S='.TXT' then begin
AssignFile(F,FileName);
Rewrite(F);
for i:=0 to Memo1.Lines.Count-1 do
WriteLn(F,Memo1.Lines[i]);
CloseFile(F);
end else if S='.BMP' then
Chart1.SaveToBitmapFile(FileName)
else if S='.WMF' then
Chart1.SaveToMetafile(FileName)
else if S='.EMF' then
Chart1.SaveToMetafileEnh(FileName)
else MessageBox(Handle,
'Valid file extensions are
''.txt'',''.bmp'',''.wmf'' and ''.emf''',
'Error',
MB_ICONSTOP OR MB_OK);
end;
end;


Mike Williams (TeamB)

unread,
Apr 2, 2008, 11:51:31 AM4/2/08
to
Alex Siron wrote:

> That is, if the user just writes "output" when the Filter "*.txt" is
> focused/active, the resulting file should be: output.txt. Likewise if
> the user just writes "output" when the Filter "*.wmf" is
> focused/active, the resulting file name should be: output.wmf.

You'll need to set the DefaultExt property. You can then write code in
the OnTypeChange event handler that updates the DefaultExt based on the
currently selected filter index.

--
-Mike (TeamB)

Remy Lebeau (TeamB)

unread,
Apr 2, 2008, 12:53:44 PM4/2/08
to

"Mike Williams (TeamB)" <mlwi!!iams@gmail!.com> wrote in message
news:xn0fogdwv...@newsgroups.borland.com...

> You'll need to set the DefaultExt property. You can then
> write code in the OnTypeChange event handler that updates
> the DefaultExt based on the currently selected filter index.

DefaultExt is only used once when Execute() initializes the dialog.
Changing the DefaultExt, such as in the OnTypeChange event, has no effect
once the dialog is active.


Gambit


Remy Lebeau (TeamB)

unread,
Apr 2, 2008, 12:51:45 PM4/2/08
to

"Alex Siron" <alex....@netti.fi> wrote in message
news:47f3...@newsgroups.borland.com...

> If the user just writes, say "output", the program raises an error
> message.

Then your code is not using the dialog's FileName value properly.

TSaveDialog has a DefaultExt property that is used when the user does not
type in any extension. Also, if you are not already doing so, you need to
use the FilterIndex property to know which item in the format filter box is
active.

> How can I put some intelligence into the coding, so that the outcome
> of the resulting output depends on recently-focused Filter of the
> SaveDialog1-box.

Use the FilterIndex property.

> That is, if the user just writes "output" when the Filter "*.txt" is
> focused/active, the resulting file should be: output.txt. Likewise
> if the user just writes "output" when the Filter "*.wmf" is
> focused/active, the resulting file name should be: output.wmf.

Use the OnTypeChange event to update the FileName field while the dialoig is
still visible. You may also need to apply an extension after Execute()
exits, if none was added by the dialog, such as if you do not have the
DefaultExt property set, ie:

procedure TForm1.SaveDialog1TypeChange(Sender: TObject);
begin
with SaveDialog1 do
begin
case FilterIndex of
0: FileName := ChangeFileExt(FileName, '.txt');
1: FileName := ChangeFileExt(FileName, '.bmp');
2: FileName := ChangeFileExt(FileName, '.wmf');
3: FileName := ChangeFileExt(FileName, '.emf');
end;
SendMessage(Windows.GetParent(Handle), CDM_SETCONTROLTEXT, 1152,
Integer(PChar(Filename)));
end;
end;

procedure TForm1.SaveAsToolButtonClick(Sender: TObject);
var
I: Integer;
begin
with SaveDialog1 do
begin
if Execute then
begin
if ExtractFileExt(FileName) = '' then
begin
case FilterIndex of
0: FileName := FileName + '.txt';
1: FileName := FileName + '.bmp';
2: FileName := FileName + '.wmf';
3: FileName := FileName + '.emf';
end;
end;
case FilterIndex of
0: Memo1.Lines.SaveToFile(FileName);
1: Chart1.SaveToBitmapFile(FileName);
2: Chart1.SaveToMetafile(FileName);
3: Chart1.SaveToMetafileEnh(FileName);
else
MessageBox(Handle, 'Unknown file type selected',
'Error', MB_ICONSTOP or MB_OK);
end;
end;
end;
end;


Gambit


Mike Williams (TeamB)

unread,
Apr 2, 2008, 1:06:01 PM4/2/08
to
Remy Lebeau (TeamB) wrote:

> DefaultExt is only used once when Execute() initializes the dialog.
> Changing the DefaultExt, such as in the OnTypeChange event, has no
> effect once the dialog is active.

My testing indicated otherwise but I now see what was happening. It
turns out that as long as *any* DefaultExt is set no extra code is
needed. Switching the filter at runtime and typing a filename without
an extension automatically applies the appropriate extension to the
resulting filename.

--
-Mike (TeamB)

Alex Siron

unread,
Apr 7, 2008, 11:34:26 AM4/7/08
to
Dear Remy Lebeau,

As you had already known, I program in Delphi 5. The code you provided below
is not working i.e. is not compiling. Error: "Undeclared identifier" for
CDM_SETCONTROLTEXT.

Unfortunately in Delphi 5 help, there is nothing there to resolve this issue
of this Msg:Cardinal. I have also already checked/searched at
www.tamaracka.com and www.deja.com, but to no avail.

In an article from tamaracka.com, it was stipulated that i must add the
"commCtrl" unit to my Uses clause for the CDM mesage constant.
I did no way.

SendMessage(Windows.GetParent(Handle),CDM_SETCONTROLTEXT,1152,Integer(PChar(Filename)));

Which Delphi 5 unit contains this constant?

Many thanks in advance.

Regards,
Alex


"Remy Lebeau (TeamB)" <no....@no.spam.com> kirjoitti
viestissä:47f3b9a3$1...@newsgroups.borland.com...

Jorge Almeida

unread,
Apr 7, 2008, 11:55:24 AM4/7/08
to
Alex Siron wrote:

> Which Delphi 5 unit contains this constant?


uses
CommDlg;

--
Jorge

Alex Siron

unread,
Apr 9, 2008, 11:14:12 AM4/9/08
to
Dear Remy Lebeau,

Warmest greetings from Finland, North Europe.
After intensive verification for the past week, it is being verified that
the code you kindly provided is not accomplishing the task at hand. The code
is always saving the results in *.bmp, what so ever. I followed all your
instructions.

Like we are all familiar with word processor like Microsoft ® Word, what is
the coding behind the saving dialog? This is just like the issue I asked
about last week. No solution yet. In Microsoft Word for example, let's run
the program, write some few texts, then we try to save.
For the file format we have the following to our dispositions:
Filter Name Filter
Text files(*.txt) *.txt
Word files('.doc) *.doc
Rtf files(*.rtf) *.rtf

Okay, we open the save-as dialog box. We find ourselves in the "Text files"
instance. We write the file name, say,
"output" with the even the extension. Microsoft Word saves to "output.txt".
Still we change the Filter Name, say, to "Word files" instance and we just
write "output". Again the program saves to "output.doc". Likewise, still we
change the Filter Name, say, to "Rtf files" instance and we just write
"output". Again the program saves to "output.doc". What are the
intelligence/coding behind this scenario? As anyone done this before in
Delphi 5?

Please help! A proper and working code will be greatly appreciated. Many
thanks in advance.

With best regards,
Alex Siron

alex....@netti.fi

"Remy Lebeau (TeamB)" <no....@no.spam.com> kirjoitti
viestissä:47f3b9a3$1...@newsgroups.borland.com...
>

Alex Siron

unread,
Apr 9, 2008, 11:20:25 AM4/9/08
to
Hello All,

Warmest greetings from Finland, North Europe.

Like we are all familiar with word processor like Microsoft ® Word, what is

the intelligent coding behind the saving dialog? This is just like the issue

I asked about last week. No solution yet. In Microsoft Word for example, let's
run the program, write some few texts, then we try to save.
For the file format we have the following to our dispositions:
Filter Name Filter
Text files(*.txt) *.txt
Word files('.doc) *.doc
Rtf files(*.rtf) *.rtf

Okay, we open the save-as dialog box. We find ourselves in the "Text files"
instance. We write the file name, say,

"output" without the even the extension. Microsoft Word saves to

"output.txt". Still we change the Filter Name, say, to "Word files" instance
and we just write "output". Again the program saves to "output.doc".
Likewise, still we change the Filter Name, say, to "Rtf files" instance and

we just write "output". Again the program saves to "output.rtf". What are

the intelligence/coding behind this scenario? As anyone done this before in
Delphi 5?

Please help! A proper and working code will be greatly appreciated. Many
thanks in advance.

With best regards,
Alex Siron

alex....@netti.fi

"Mike Williams (TeamB)" <mlwi!!iams@gmail!.com> kirjoitti
viestissä:xn0fogdwv...@newsgroups.borland.com...

0 new messages