1

According to our requirements, we just want to enable the user to select a place to save a file and restrict the editing of its name.

Is it possible to that with inbuilt SaveDialog component of Delphi (7)?

Learner
  • 53
  • 6

1 Answers1

10

Yes, it is possible, but that isn't what you really want to do. What you want to do is not to select a file name, but to select a folder, and that is a different problem.

Using FileCtrl,

var
  dir: string;
begin
  if SelectDirectory('Select Directory','', dir, [sdNewUI, sdNewFolder]) then
    ShowMessage(dir);

On Vista+ you can also use the directory selection mode of the file dialog.

Community
  • 1
  • 1
Andreas Rejbrand
  • 105,602
  • 8
  • 282
  • 384
  • I think you're right about the real goal here, but if it is possible to do exactly what the OP asked, I would be interested to know the answer. – Thijs van Dien May 03 '13 at 09:39
  • @ThijsvanDien: I think you only have to send a [`EM_SETREADONLY`](http://msdn.microsoft.com/en-us/library/windows/desktop/bb761655(v=vs.85).aspx) message to the EDIT control. – Andreas Rejbrand May 03 '13 at 09:44
  • That would keep the user from typing in a different name, but clicking an existing file would also cause the content to change. – Thijs van Dien May 03 '13 at 09:48
  • @ThijsvanDien: True. Anyhow, that only emphasises my main point: This isn't what the OP *really* wants to do. – Andreas Rejbrand May 03 '13 at 09:49
  • 3
    +1 It's actually not possible to do what @Learner wants. Yes you can make edit control read-only, if you can find it. But that in itself requires hacking and relies on implementation detail. Could change in future Windows. And it won't stop the filename being changed by clicking on the list view. So if I were you I would say "it cannot reasonably be done your way, the right way is to ask user to select folder". – David Heffernan May 03 '13 at 10:05
  • 3
    In the dialog's `OnCanClose` event, you can check the entered filename and simply not allow the dialog to close if it does not match the intended value. – Remy Lebeau May 03 '13 at 16:58
  • @Remy We can make use of onCanClose Event and we can make user to use only the name we want. But there is a problem with this approach , What if we want to put a original file name that we want the user to enter. Suppose i put the name in first occurrence and user edited it , We stopped the user .But after that is it possible to show the name in Edit box to user which we want him to use ? – Learner May 06 '13 at 08:12
  • procedure TForm1.SaveDialog1CanClose(Sender: TObject; var CanClose: Boolean); var location:String; name:String; begin name:=ExtractFileName(SaveDialog1.FileName); location:=ExtractFileDir(SaveDialog1.FileName); if name<>'Itstestfile.txt' then begin CanClose:=False; SaveDialog1.FileName:=location+'\Itstestfile.txt'; //is it possible to enter the original file name again in the edit box ShowMessage('can not change the file name'); end else begin CanClose:=True; end; end; – Learner May 06 '13 at 08:23
  • @Learner: yes, it is possible, but not with the `FileName` property. You have to send a `CDM_SETCONTROLTEXT` message to the dialog instead: `SendMessage(GetParent(SaveDialog1.Handle), CDM_SETCONTROLTEXT, edt1, LPARAM(PChar(location+'\Itstestfile.txt')));` or `CommDlg_OpenSave_SetControlText(GetParent(SaveDialog1.Handle), edt1, PChar(location+'\Itstestfile.txt'));` – Remy Lebeau May 06 '13 at 15:37