We help IT Professionals succeed at work.

TSaveDialog and filename

michaenh
michaenh asked
on
2,145 Views
Last Modified: 2012-06-27
Hi Delphi Experts.

Ok. Here is the question...
I want to change the filename in the SaveDialogbox  when users choose different.. filter.
f.ex. filter as txt, xml, csv etc. and the filename will appears as MyFile.txt, MyFile.xml, etc.

I manage to change the filename before SaveDialog is executed but not while it is executing(when the dialog is open fo the user).

I could create my own dialogsbox but it would be easier to use TSaveDialog..

cheers and many thanks
mha
Comment
Watch Question

Top Expert 2004

Commented:
what about to use the ontypechange-event?

Author

Commented:
Hi kretzschmar.. :)

It does not help when I add this code on the ontypechange-event or on the SelectionChange

 case SaveDialog1.FilterIndex of
 1: SaveDialog1.FileName:=filename+'.txt'
 2: SaveDialog1.FileName:=filename+'.xml'
.
.
 end;

when i tried with showMessage in the case statement then I got correct messages.
It seems that savedialog.filename can not be overwritten when the dialog has been executed...

any more ideas..
thanks

cheers,
mha


Top Expert 2004

Commented:
hmm, seems the dialog is a bit buggy,
thought it could go this way

procedure TForm1.Button1Click(Sender: TObject);
begin
  if savedialog1.execute then
    showmessage(savedialog1.filename);
end;

procedure TForm1.SaveDialog1TypeChange(Sender: TObject);
begin
  case savedialog1.FilterIndex of
    1 : savedialog1.DefaultExt := 'xml';
    2 : savedialog1.DefaultExt := 'dat';
    3 : savedialog1.DefaultExt := 'txt';
  else
    savedialog1.DefaultExt := '';
  end;
end;

but the onTypeChange-event is only triggered, if i select
the first filter-entry

well, looking for a solution

meikl ;-)

Author

Commented:
hi

>hmm, seems the dialog is a bit buggy,
Yes it seems so..
>thought it could go this way
I also thought the above code you wrote would work..
>well, looking for a solution
Really appriaceted!

thanks once again

mha:)






This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Hi there Shane

Still some strange problem with it..
I will try to take a further look at it...

thanks

mha

Commented:
You can do this in one line...

Example of saving to file..

...savetoFile(ExtractFilePath(SaveDialog1.FileName) + 'MyFile' + InTToStr(savedialog1.FilterIndex));

or.. if above don`t want to work:

...savetoFile(ExtractFilePath(SaveDialog1.FileName) + '\' + 'MyFile' + InTToStr(savedialog1.FilterIndex));

There can be some problems with chars as dots and '\' .. but there are just detail :)

Author

Commented:
Hi Deti.

There is no problem with saveTofile...
but the problem is more like as meikl reffered
> onTypeChange-event is only triggered, if i select the first filter-entry

Shane's method was ok.
Ex.
procedure TForm1.Button1Click(Sender: TObject);
begin
 savedialog1.Filter:='html|*.html|txt|*.txt|dat|*.dat|pdf|*.pdf|csv|*.csv';
 SaveDialog1.DefaultExt:='';
 if savedialog1.execute then
   begin
    SaveDialog1TypeChange(self);
    showmessage( SaveDialog1.FileName + '.' + SaveDialog1.DefaultExt);
   // Here you can saveTofile...
   end;
end;

procedure TForm1.SaveDialog1TypeChange(Sender: TObject);
begin
  case savedialog1.FilterIndex of
    1 : savedialog1.DefaultExt := 'html';
    2 : savedialog1.DefaultExt := 'txt';
    3 : savedialog1.DefaultExt := 'dat';
    4 : savedialog1.DefaultExt := 'pdf';
    5 : savedialog1.DefaultExt := 'csv';
  else
    //savedialog1.DefaultExt := '';
  end;
end;

I only miss that TSaveDialog should be more as f.ex save as in MS Word.
The extension is added to filename when ever i choose a spefic filter.. thanks for all your help.

cheers,
mha

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.