We help IT Professionals succeed at work.

How to store a path in EditBox1.Text via DialogBox control?

IT79637
IT79637 asked
on
I'm using EditBox1 to store path information where files are located.  The end user can type the path or copy/paste the path into the EditBox.  How do I give the end user the option to search for the path a dialog much like "Save As", but w/0 the file name.  I only want path.

Thanks
Comment
Watch Question

Depending on the version of Delphi you are using, it should be easy.
There is probably a Win 3.1 tab in your components toolbar.  That one should have, among other things, a Drive Combobox, a Filter Combobox, and a Directory Listbox.  I don't know why they have not been upgraded like many other components (it's not like people don't still need the functionality ;-) but that is where they most commonly are found.
You can link each of them to thenext higher level, e.g. Directory Listbox to Drive Combobox, so that the controls cascade.
Commented:
hi,   try that code;
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  s:string;

implementation
uses ShlObj;

{$R *.dfm}


function BrowseCallbackProc(hwnd: HWND; uMsg: UINT; lParam: LPARAM; lpData: LPARAM): Integer; stdcall;
begin
  if (uMsg = BFFM_INITIALIZED) then
    SendMessage(hwnd, BFFM_SETSELECTION, 1, lpData);
  BrowseCallbackProc := 0;
end;

function GetFolderDialog(Handle: Integer; Caption: string; var strFolder: string): Boolean;
const
  BIF_STATUSTEXT           = $0004;
  BIF_NEWDIALOGSTYLE       = $0040;
  BIF_RETURNONLYFSDIRS     = $0080;
  BIF_SHAREABLE            = $0100;
  BIF_USENEWUI             = BIF_EDITBOX or BIF_NEWDIALOGSTYLE;

var
  BrowseInfo: TBrowseInfo;
  ItemIDList: PItemIDList;
  JtemIDList: PItemIDList;
  Path: PAnsiChar;
begin
  Result := False;
  Path := StrAlloc(MAX_PATH);
  SHGetSpecialFolderLocation(Handle, CSIDL_DRIVES, JtemIDList);
  with BrowseInfo do
  begin
    hwndOwner := GetActiveWindow;
    pidlRoot := JtemIDList;
    SHGetSpecialFolderLocation(hwndOwner, CSIDL_DRIVES, JtemIDList);

    { return display name of item selected }
    pszDisplayName := StrAlloc(MAX_PATH);

    { set the title of dialog }
    lpszTitle := PChar(Caption);//'Select the folder';
    { flags that control the return stuff }
    lpfn := @BrowseCallbackProc;
    { extra info that's passed back in callbacks }
    lParam := LongInt(PChar(strFolder));
  end;

  ItemIDList := SHBrowseForFolder(BrowseInfo);

  if (ItemIDList <> nil) then
    if SHGetPathFromIDList(ItemIDList, Path) then
    begin
      strFolder := Path;
      Result := True
    end;
end;



procedure TForm1.Button1Click(Sender: TObject);
begin
s:=ExtractFilePath(Application.ExeName);
if GetFolderDialog(Application.Handle, 'Select a folder', s) then
begin
  Edit1.Text := s;
  ShowMessage('User selected this folder: ' + s);
  end;
end;

end.

Open in new window

Commented:
Use opendialog componet to get filepath. when you select the file to open you get full path of the file and then you need to remove only the filename from that path information
 

Commented:
you can ue this function too.
 
function GetLocation: String;
begin
  Result := ExtractFileDir(filename);
end;

Commented:
>>but w/0 the file name

Commented:
Another1;
procedure TForm1.Button1Click(Sender: TObject);
var
  Dir: String;
begin
  SelectDirectory('Select a directory', '', Dir);
  Edit1.Text := Dir;
  ShowMessage(Dir);
end; 

Open in new window

Author

Commented:
Increased points to 500.

Author

Commented:
I allocated points equally to each respondent.