Solved

listbox action

Posted on 2001-07-28
7
244 Views
Last Modified: 2010-04-06
I would like to know how to read every line from a listbox and then doing something. For example, the listbox has the full path names of files...and then the user clicks a button and these files in the listbox is deleted.
0
Comment
Question by:marinedestroyer2
7 Comments
 
LVL 5

Expert Comment

by:alanwhincup
Comment Utility
You could do it like this:

To use it just put a ListBox and Button component on a form and put the following code under the Button's OnClick event.

procedure TForm1.Button1Click(Sender: TObject);
var
  I : Integer;
begin
  if ListBox1.Items.Count > 0 then
  begin
    for I := 0 to ListBox1.Items.Count - 1 do
    begin
      if FileExists(ListBox1.Items[I]) = True then
        DeleteFile(ListBox1.Items[I])
      else
        ShowMessage('File does not exist.');
    end;
  end
  else
    ShowMessage('No files listed.');
end;

Cheers,

Alan
0
 
LVL 22

Expert Comment

by:mnasman
Comment Utility
Hello

  if you want to delete every file in the list box by click on it, just add this code in onclick event

procedure TForm1.ListBox1Click(Sender: TObject);
begin
  DeleteFile(ListBox1.Items.Strings[ListBox1.ItemIndex]);
end;

but if you want to select some items then to delete them i on click, set the MulitSelect for Listbox to true, and add button and write this code

procedure TForm1.Button1Click(Sender: TObject);
  var
    I : integer;
begin
  for I := 0 to ListBox1.Items.Count -1 do
    if ListBox1.Selected[I] then
      DeleteFile(ListBox1.Items.Strings[I]);

end;
Mohammed
0
 
LVL 1

Expert Comment

by:h_mohsenian
Comment Utility
hi , marinedestroyer2

i had written your answer at the end of your last question.

hamed
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 1

Expert Comment

by:h_mohsenian
Comment Utility
if it is related your last question!
0
 
LVL 5

Accepted Solution

by:
alanwhincup earned 100 total points
Comment Utility
Here is an improved version of my example:

procedure TForm1.Button1Click(Sender: TObject);
var
  I : Integer;
begin
  if ListBox1.Items.Count > 0 then
  begin
    I := 0;
    while I <> ListBox1.Items.Count do
    begin
      if FileExists(ListBox1.Items[I]) = True then
      begin
        if DeleteFile(ListBox1.Items[I]) = False then
          ShowMessage('File "' + ListBox1.Items[I] + '" could not be deleted.')
        else
        begin
          ListBox1.Items.Delete(I);
          Dec(I);
        end;
      end
      else
        ShowMessage('File "' + ListBox1.Items[I] + '" does not exist.');
      Inc(I);
    end;
    if ListBox1.Items.Count = 0 then
      ShowMessage('All files deleted succesfully.');
  end
  else
    ShowMessage('No files listed.');
end;
0
 
LVL 26

Expert Comment

by:Russell Libby
Comment Utility
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept alanwhincup's comment as answer

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Thank you,
Russell

EE Cleanup Volunteer
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now