Recent

Author Topic: New to the Forum!  (Read 7621 times)

DumDum

  • New Member
  • *
  • Posts: 32
Re: New to the Forum!
« Reply #15 on: December 25, 2017, 05:52:18 am »
You are the man Howard!

I will dig into this further and learn from it as much as I can. 

Thanks for all your help so far guys!

Merry Christmas/Happy Holidays to all :)

DumDum

  • New Member
  • *
  • Posts: 32
Re: New to the Forum!
« Reply #16 on: December 25, 2017, 07:24:57 am »
Now say if I wanted to make the file size fetch conditional to a button press rather than just automatically populating it on drag and drop...

My train of thought is around an "if" statement that would be conditional on Button1Click(Fetch File Size) being true/false.

Where if the button click is true (button pressed) it will jump to a function that will display file size of last file loaded.

Is this something that is possible?

I have tried using the following

procedure TForm1.Button1Click(Sender: TObject);

var

filePathAndName, fName: String;
  sz: Int64;
  D: QWord = 1024;

begin

    fName := ExtractFileName(filePathAndName);
    sz:=FileSize(filePathAndName)DIV d;
    SizeBox.Items.Add('%s has size %dKB',[fName, sz]);

end; 

I understand the shortcoming is that there is no file to compare with so the result keeps coming back as file size is 0kb.  I am just curious to know if there is a way to call down from the original function of drag and drop and somehow saving the file that was loaded in the previous function for long enough to fetch its size in the next function.

C
« Last Edit: December 25, 2017, 08:12:22 am by CDuman »

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: New to the Forum!
« Reply #17 on: December 25, 2017, 05:54:39 pm »
My suggestion would be to allow multiple files to be dragged and shown in the listbox. If you did not clear the listbox either, any newly dropped files would be appended rather than replacing the existing content. Or you could keep a separate list of historic dropped files.
Either way, rather than adding a button to clutter the interface, why not just use the listbox selection change to update display of the file size? The selected file can be changed either by clicking on a listbox item, or by using the keyboard arrow keys.
See the attached project for one way to do this.

DumDum

  • New Member
  • *
  • Posts: 32
Re: New to the Forum!
« Reply #18 on: December 27, 2017, 05:31:51 pm »
Thanks for the reply Howard, most of the code written is super intimidating to me right now.  I will have to give this some study and thought.

Is there no way to make a condition based off a button being clicked or not when fetching a file size?

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: New to the Forum!
« Reply #19 on: December 27, 2017, 05:38:18 pm »
Thanks for the reply Howard, most of the code written is super intimidating to me right now.  I will have to give this some study and thought.

Is there no way to make a condition based off a button being clicked or not when fetching a file size?
there are many buttons types and innumerable ways to do that. You already have the code to get the file size of any filename. What is the problem exactly you have? you can not get the filesize? you can not show the filesize on screen? the file size returned is not correct? the way it is returned is not correct? you do not know how to access the filename you need?
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

DumDum

  • New Member
  • *
  • Posts: 32
Re: New to the Forum!
« Reply #20 on: December 27, 2017, 05:55:24 pm »
I want to show the file size on the screen only when I hit another button.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: New to the Forum!
« Reply #21 on: December 27, 2017, 08:19:00 pm »
Is there no way to make a condition based off a button being clicked or not when fetching a file size?

Yes, of course. You could adapt the code I gave earlier.
Remove the OnSelectionChange method (in interface and implementation sections).
Add a button somewhere named FileSizeButton, and double click to button to generate an OnClick handler.
Alter the OnDropFiles and complete the OnClick as follows:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FilesizeButtonClick(Sender: TObject);
  2.  
  3.   function GetFirstSelectedDragBoxItem: String;
  4.   var
  5.     i: Integer;
  6.   begin
  7.     for i := 0 to DragBox.Count-1 do
  8.       if DragBox.Selected[i] then
  9.         Exit(DragBox.Items[i]);
  10.     Result := '';
  11.   end;
  12.  
  13. var
  14.   s: String;
  15. begin
  16.   if DragBox.Count > 0 then begin
  17.     SizeBox.Clear;
  18.     s := GetFirstSelectedDragBoxItem;
  19.     if s = '' then
  20.       s := DragBox.Items[0];
  21.     SizeBox.Items.Add( Format('%s has size %d KB', [ExtractFileName(s), FileSize(s) div 1024]) );
  22.   end
  23.   else
  24.     SizeBox.Items.Add('<No file>');
  25. end;
  26.  
  27. procedure TForm1.FormDropFiles(Sender: TObject; const FileNames: array of String);
  28. var
  29.   i: Integer;
  30. begin
  31.   if Length(FileNames) > 0 then begin
  32.     DragBox.Clear;
  33.     SizeBox.Clear;
  34.     for i := 0 to High(FileNames) do
  35.       DragBox.Items.Add(FileNames[i]);
  36.   end;
  37. end;                    

DumDum

  • New Member
  • *
  • Posts: 32
Re: New to the Forum!
« Reply #22 on: December 27, 2017, 10:49:50 pm »
Thanks Howard,

I will give it a whirl :)

 

TinyPortal © 2005-2018