Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OpenDialog filename to Editbox

Status
Not open for further replies.

Ante0

Programmer
Apr 19, 2007
98
SE
Hi, is there a way to get a OpenDialog Filename to show in an Edit or ListBox, but not show Fullpath, only the FileName in Delphi 7. I.e. Instead of D:\Folder\Bla.file it should only show as Bla.file.
I'm making an Mp3 player, and the ListBox I'm using as a Playlist doesn't "show" the filename when the path to the file is too long. It does show it though, but I want my app as samll as possible, and resizing would kill it :p

Thanks,
Ante0
 
Code:
if OpenDialog1.Execute then
Edit1.Text:=ExtractFilename(OpenDialog1.FileName);

you can even make your strings like this
C:\My Music\Per..\Spin.Mp3 with this function
Code:
function Mince(PathToMince :String; InSpace :Integer): String;
var TotalLength, FileLength, FLength : Integer;
    FileName,Path :String;
begin
 FileName:=ExtractFileName(PathToMince);
 Path:=ExtractFilePath(PathToMince);
 TotalLength:=Length(PathToMince);
 FileLength:=Length(FileName);
  if TotalLength > InSpace then
   begin
    FLength := (Inspace-FileLength) - 4;
    Result := Copy(PathToMince, 0, fLength)+ '...\'+ FileName;
   end
  else
   Result := PathToMince;
end;
The "InSpace" is the number of characters the intended space can hold.

Aaron
 
Thanks Aaron, for your reply.
I think I'll just stick with the ExtractFileName for now, this is a simple app, and I tend to keep it that way. (I'm a newbie, it sounded better the other way :p)
One question though about the Function...
If I make an option to Save playlist, will the full path's to the files still be saved?
I'm using a ListBox now. Might need something different for that. But I'll just stick with a Label showing the current track with ExtractFileName, and the fullpath's still in the playlist.

Thanks again,
Ante0
 
Basically what you have are just strings, so if you use ExtractFileName to get just 'bla.file' then put that in your playlist, then your playlist won't know the full path info of the files when you come to save it.
If you just want to show the name of the file in the playlist, but keep the full path info somewhere that the user doesn't see it, then you can use a TListBox to do this.
As you know a TListBox maintains a list of strings, but you may not know that each string may have an associated TObject. It is possible to typecast a string as a TObject and hence another string (that the user won't see) may be associated with each string in the list. Here's an example of how to add file info into a TListBox and then get it out again:

Code:
procedure TForm1.bnOpenFileClick(Sender: TObject);
var
  TrackName: string;
  FullTrackPath: string;
begin
  if OpenDialog1.Execute then
  begin
    FullTrackPath := OpenDialog1.FileName;
    TrackName := ExtractFileName(FullTrackPath);
    ListBox1.Items.AddObject(TrackName, TObject(FullTrackPath));
  end;
end;

procedure TForm1.bnShowTrackInfoClick(Sender: TObject);
var
  TrackName: string;
  FullTrackPath: string;
begin
  TrackName := ListBox1.Items[ListBox1.ItemIndex];
  FullTrackPath := (string(ListBox1.Items.Objects[ListBox1.ItemIndex]));

  ShowMessage(Format('Name: %s, Path: %s', [TrackName, FullTrackPath]));
end;

Hopefully that all made sense.

Steve
 
Ooh, that I did not know. I'll get right onto it! :)
I am using a ListBox already, and was not using ExtractFileName with it. I just used ExtractFileName with a label, so it just showed the filename there, and the listbox had the full names in them. Using folders/files with long names didn't work too good though, since you don't know what song played, or what the next song would be.
But as I said, I'll get right onto this new code :)
Thanks
 
I can't seem to find any Edit button on these forums, so I'll have to double post.
The Code works great Steve!
I've been searching a long time for something as simple as this.
Now to tackle my next problem... hehe.
It's not really important, just making the player simpler.
About adding multiple songs at once, I've set OpenDialog to allow multi selections.
And I was using this code:
Code:
procedure TForm2.AddClick(Sender: TObject);
var
i: integer;
begin
if OpenDialog1.Execute then
for i:=0
to OpenDialog1.Files.Count-1 do
Playlistbox.Items.Add(Opendialog1.Files[i]);
if PlayListBox.Items.Count > 0 then
begin
Form1.MediaPlayer1.FileName := Playlistbox.Items.Strings[0];
Form1.MediaPlayer1.Open;
Form1.Play1.Enabled := True;
Form1.Next1.Enabled := True;
Form1.Prev1.Enabled := True;
end
else
exit;
end;

This will not work with associating strings in Listbox with Objects.
Anyone got any ideas?
I will keep on trying though, just now it feels like I'm stuck. And it's not really important if allowing multiple selections wouldn't work, I'm only doing this to learn :)
 
What do you mean by "will not work"? If you want to allow multiple selections you can do something like this:

Code:
procedure TForm2.AddClick(Sender: TObject);
var
  i: integer;
begin
  if OpenDialog1.Execute then
    for i:=0 to OpenDialog1.Files.Count-1 do
      Playlistbox.Items.AddObject(ExtractFileName(Opendialog1.Files[i]), TObject(OpenDialog1.Files[i]));

  if PlayListBox.Items.Count > 0 then
  begin
    Form1.MediaPlayer1.FileName := string(Playlistbox.Items.Objects[0]);
    Form1.MediaPlayer1.Open;
    Form1.Play1.Enabled := True;
    Form1.Next1.Enabled := True;
    Form1.Prev1.Enabled := True;
  end
  else
    exit;
end;

Steve
 
Ah, That's about the same code I tried with... Must've forgotten something.
Thanks again Steve, and sorry for my stupid questions. But hey, we're all newbies at some point. hehe
 
No problem. As you've probably noticed, the only differences between your code and mine were that I used AddObject instead of Add to add items to the listbox, and then used Items.Objects[0] instead of Items.Strings[0] to retrieve the full path name of the file

Steve
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top