Recent

Author Topic: Custom FileListBox [SOLVED by jamie]  (Read 2204 times)

totya

  • Hero Member
  • *****
  • Posts: 720
Custom FileListBox [SOLVED by jamie]
« on: May 14, 2018, 10:17:10 pm »
Hi!

I want a simple filelistbox, but not with initial directory, I want to add file to the list via file open dialog (or delete/empty). But Important, I want to see only the filenames in the listbox (without path). But if I read any listboxitem, I want the full filename (with path). Is there any already existing solution of this situation?

Thank you!
« Last Edit: May 15, 2018, 06:58:32 am by totya »

ASerge

  • Hero Member
  • *****
  • Posts: 2246
Re: Custom FileListBox
« Reply #1 on: May 14, 2018, 10:23:49 pm »
I want a simple filelistbox, but not with initial directory, I want to add file to the list via file open dialog (or delete/empty). But Important, I want to see only the filenames in the listbox (without path). But if I read any listboxitem, I want the full filename (with path). Is exist any solution of this situation?
If you want to add or remove file names yourself, i.e. you do not need to reflect the real contents of the folder, then use a regular TListBox.

jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: Custom FileListBox
« Reply #2 on: May 14, 2018, 11:49:37 pm »
Use a Tlistbox box and implement the OnDrawItem...

 In that event, you can display the file name in the list using the ExtractFileName(ListBox.Items[The_Index_];

 So when you populate the list you can add the full path but only show the FileName.....

 If you need example, ask.
The only true wisdom is knowing you know nothing

totya

  • Hero Member
  • *****
  • Posts: 720
Re: Custom FileListBox
« Reply #3 on: May 15, 2018, 12:02:23 am »
Use a Tlistbox box and implement the OnDrawItem...

 In that event, you can display the file name in the list using the ExtractFileName(ListBox.Items[The_Index_];

 So when you populate the list you can add the full path but only show the FileName.....

 If you need example, ask.

Hi, my first thing is the TStrings buffer, but seems to mee your idea is simpler than my. Please show example for me.
« Last Edit: May 15, 2018, 12:04:50 am by totya »

jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: Custom FileListBox
« Reply #4 on: May 15, 2018, 01:55:41 am »
Code: Pascal  [Select][+][-]
  1. uses
  2.   lclType,Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, Types;
  3.  
  4. type
  5.  
  6.   { TForm1 }
  7.  
  8.   TForm1 = class(TForm)
  9.     ListBox1: TListBox;
  10.     procedure ListBox1DrawItem(Control: TWinControl; Index: Integer;
  11.       ARect: TRect; State: TOwnerDrawState);
  12.   private
  13.  
  14.   public
  15.  
  16.   end;
  17.  
  18. var
  19.   Form1: TForm1;
  20.  
  21. implementation
  22.  
  23. {$R *.lfm}
  24.  
  25. { TForm1 }
  26.  
  27. procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  28.   ARect: TRect; State: TOwnerDrawState);
  29. begin
  30.   With TListBox(Control) Do
  31.    begin
  32.      if (Focused)and(OdSelected in State) Then
  33.       Begin
  34.        Canvas.Brush.Color :=  clBlue;
  35.        Canvas.Font.COlor :=   ClWhite;
  36.       End Else
  37.       Begin
  38.       Canvas.Brush.COlor := brush.color;
  39.       Canvas.Font.Color := Font.color;
  40.       end;
  41.      Canvas.FillRect(Arect);
  42.      Canvas.TextOut(Arect.Left,Arect.Top,ExtractFileName(Items[index]));
  43.    end;
  44. end;
  45.  

Make sure you set the STYLE property in the listbox to ilOwnerDrawFixed;

You can change the colors to your liking, that is what I used for the moment :)
The only true wisdom is knowing you know nothing

totya

  • Hero Member
  • *****
  • Posts: 720
Re: Custom FileListBox
« Reply #5 on: May 15, 2018, 06:57:27 am »
Hi!

Thanks for the sample code! And its works.

Small remark, I think this code more convient:

Code: Pascal  [Select][+][-]
  1. Canvas.Brush.Color :=  clHighlight;
  2. Canvas.Font.Color :=   clHighlightText;

 

TinyPortal © 2005-2018