-1

In my VCL form application I have added:

  1. Button
  2. OpenDialog
  3. Edit
  4. ListView

When I open a file with opendialog, how do i display the file size and format in ListView column?

Here is the code which am trying - please correct my mistake, or what function do I have to add to display in ListView?

//===============================
procedure TForm1.BntOpen1Click(Sender: TObject);
var
  LI: TListItem;
begin
   if OpenDialog1.Execute then
     LI := ListView1.Items.Add;
    LI.SubItems.Add(Format(OpenDialog1.FileName, [0]));
end;
//===========================

thanks

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
meitei
  • 27
  • 4
  • Please help us by making your question much more clear. That will involve spending some time explaining in more detail what you are stuck on. Also, "n" is not a word. – David Heffernan Apr 29 '16 at 02:28
  • What's missing is a OpenDialog. Then write some code in the button click handler. – Sertac Akyuz Apr 29 '16 at 03:06
  • I edited the code formatting for you. If you include also the type definition of your form, you can delete your list of what components you have. – Tom Brunberg May 01 '16 at 05:58
  • Sorry friends for my bad English.. But now I have edited my question.I hope you guys will understand . – meitei May 01 '16 at 05:59
  • @Tom if you don't mind can you post your code . – meitei May 01 '16 at 06:05
  • That is not how SO works, we are not a code writing service. But answer the following: Why is there a `TEdit`. Read up in [Delphi reference](http://docwiki.embarcadero.com/RADStudio/XE7/en/Delphi_Reference) compound statements, (or code blocks (`begin ... end;`)) and about the `Format()` function. – Tom Brunberg May 01 '16 at 06:12
  • @Tom sorry I didn't add a lines in my code. TEdit is for displaying the file which I choose from a directory.//...edit1.text:=opendialog1.filename;...//I know am not good in coding as you guys i make mistake alot because am just a learner so my humble request is to help or guide me . – meitei May 01 '16 at 07:25

1 Answers1

0

First, you are missing a begin .. end; pair.

begin
  if OpenDialog1.Execute then 
  begin  // missing
    // ...
    // fetch file size and update `TListView` here
    // ...
  end;  // missing
end;

Then to get the file size, declare a variable size: int64; and use a TFileStream

var
  size: int64;
...
    fs := TFileStream.Create(OpenDialog1.FileName, fmOpenRead or fmShareDenyWrite);
    try
      size := fs.Size;
    finally
      fs.Free;
    end;

Be sure to read up on file mode and sharing constants

Have you used the Column Designer of the listview to declare the columns you need? If not, right-click on the TListView and select Column Designerfrom the menu.

You correctly added an item to the listview but you never assigned a caption to it, so I would assign the filename, so it would be clear to which file the size and type are related.

  LI.Caption := OpenDialog1.FileName;

Then you add the size of the file into a subitem

  LI.SubItems.Add(IntToStr(size));

and finally the file type

  LI.SubItems.Add(ExtractFileExt(OpenDialog1.FileName));
Tom Brunberg
  • 20,312
  • 8
  • 37
  • 54
  • thanks a lot for your great help .Now its working as I want it really you are awesome...thank you. – meitei May 01 '16 at 14:21
  • @meitei Glad I could help. Now, I can see that you have not yet visited [**the tour**](http://stackoverflow.com/tour) which describes in short terms how the SO site works, please do so. And also take a look at [**what should I do if somebody answers**](http://stackoverflow.com/help/someone-answers). – Tom Brunberg May 01 '16 at 14:35
  • am new user to this apps that's why i make mistake but now I came to know by your advice. In future I will try not to make mistakes and to follow as you said. – meitei May 01 '16 at 16:55