Solved

Listbox Manipulating

Posted on 2010-09-08
14
967 Views
Last Modified: 2012-05-10
I wonder how I can add one. Dll file in my ListBox and that the file listbox to go with your name and format. Dll.

And also to remove the item from the ListBox.

And how can I save the files that I insert in the program for listbox when I open the items are in the listbox

Thanks
0
Comment
Question by:nkilla
  • 5
  • 5
  • 4
14 Comments
 
LVL 7

Expert Comment

by:samenglish
ID: 33633303
I don't understand what DLLs have to do with this... isn't it just another filename you want to add to your listbox (nevermind whether it's a DLL or not). Keep things simple.

Place 4 TButtons, 1 TListBox, 1 TOpenDialog and 1 TSaveDialog on a form
Example code attached. Hope it helps. Sam.


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    OpenDialog1: TOpenDialog;
    SaveDialog1: TSaveDialog;
    btnRemove: TButton;
    btnSave: TButton;
    btnAdd: TButton;
    btnClose: TButton;
    procedure Button1Click(Sender: TObject);
    procedure btnAddClick(Sender: TObject);
    procedure btnRemoveClick(Sender: TObject);
    procedure btnCloseClick(Sender: TObject);
    procedure btnSaveClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  self.ListBox1.DeleteSelected;
end;

procedure TForm1.btnAddClick(Sender: TObject);
begin
  OpenDialog1.Execute;
  ListBox1.Items.Add(OpenDialog1.Filename);
end;

procedure TForm1.btnRemoveClick(Sender: TObject);
begin
  ListBox1.DeleteSelected;
end;

procedure TForm1.btnCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TForm1.btnSaveClick(Sender: TObject);
begin
  SaveDialog1.Execute;
  ListBox1.Items.SaveToFile(SaveDialog1.Filename);
end;

end.

Open in new window

0
 
LVL 7

Expert Comment

by:samenglish
ID: 33633306
Oh... set Multiselect = True on the ListBox and you can select multiple items with your mouse while holding down the Ctrl key.
0
 
LVL 25

Expert Comment

by:epasquier
ID: 33634344
For the saving/loading part , you can use an inifile named after your application

Here is the corresponding code (to merge with samenglish code)
  TForm1 = class(TForm)

    procedure FormClose(Sender: TObject; var Action: TCloseAction);

    procedure FormCreate(Sender: TObject);

  private

    { Private declarations }

    procedure LoadParams;

    procedure SaveParams;

  public

    { Public declarations }

  end;





procedure TForm1.FormCreate(Sender: TObject);

begin

 LoadParams;

end;



procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);

begin

 SaveParams;

end;





procedure TForm1.LoadParams;

Var

 FileName:String;

 IniFile:TIniFile;

 i,N:integer;

begin

 FileName:=ChangeFileExt(Application.ExeName,'.INI'); 

 IniFile:=TIniFile.Create(FileName);

 N:=IniFile.ReadInteger('DLL_LIST','COUNT',0);

 ListBox1.Items.Clear;

 for i:=1 to N do 

  begin 

   FileName:=IniFile.ReadString ('DLL_LIST',IntToStr(i),'');

   if FileExists(FileName) Then ListBox1.Items.Add(FileName);

  end;

 IniFile.Free;

end;



procedure TForm1.SaveParams;

Var

 FileName:String;

 IniFile:TIniFile;

 i,N:integer;

begin

 FileName:=ChangeFileExt(Application.ExeName,'.INI');

 IniFile:=TIniFile.Create(FileName);

 IniFile.WriteInteger('DLL_LIST','COUNT',ListBox1.Items.Count);

 for i:=0 to ListBox1.Items.Count-1 do 

  begin 

   IniFile.WriteString ('DLL_LIST',IntToStr(i+1),ListBox1.Items[i]);

  end;

 IniFile.Free;

end;

Open in new window

0
 

Author Comment

by:nkilla
ID: 33639795
The Save I'd like to save the current list and she quit the program begin with the files saved in the list.

Another thing, and that when adding a new item, I wanted to appear only the Item Name and not the path of the item.

As appears above
capturechh.jpg
0
 
LVL 25

Expert Comment

by:epasquier
ID: 33640223
> The Save I'd like to save the current list and she quit the program begin with the files saved in the list.
If I understand correctly : you want the list to be saved when you quit the application.
That is exactly what I did in the former post

for the other part, you will need to save the full-qualified name list in another list. I'll do that tomorrow
0
 
LVL 7

Expert Comment

by:samenglish
ID: 33642433
1. Use ExtractFileName function from SysUtils to extract filename from filepath.

2. TListBox.Items is of type TStrings. TStrings can easily be saved to or loaded from a text file using either SaveToFile or LoadFromFile method respectively.

3. Place code in the form's OnCreate or OnShow event handler to perform operations while the form is "loading", and in the form's OnClose event handler to perform operations when closing the form. The event handler can be accessed from the Events tab of the Object Inspector.

4. Your question has been answered... Or, did you think we could build the whole app for you as you continue to change the requirements?

5. It looks like you're making a potentially malicious tool... You should use your power for good not evil :-)
I cannot continue in good conscience. Adios amigo.

6. I don't like usernames that sound "gangsta". Peace out homie. :-)
0
 
LVL 25

Expert Comment

by:epasquier
ID: 33642783
I agree with points 1..4 of samenglish, but don't understand points 5/6

Here is the code for all we said before :
* based on samenglish code to add dll to list
* adding support for shortnames in list box + longnames in stringlist
* load/save in inifile at loading / closing of form
unit Unit1;



interface



uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls;



type

  TForm1 = class(TForm)

    DllListBox: TListBox;

    DllOpenDialog: TOpenDialog;

    btnRemove: TButton;

    btnSave: TButton;

    btnAdd: TButton;

    btnClose: TButton;

    procedure btnAddClick(Sender: TObject);

    procedure btnRemoveClick(Sender: TObject);

    procedure btnCloseClick(Sender: TObject);

    procedure btnSaveClick(Sender: TObject);

    procedure FormCreate(Sender: TObject);

    procedure FormClose(Sender: TObject; var Action: TCloseAction);

    procedure FormDestroy(Sender: TObject);

  private

    { Private declarations }

    _DllList:TStringList;



    procedure LoadParams;

    procedure SaveParams;

 end;



var

  Form1: TForm1;



implementation



{$R *.dfm}



procedure FormCreate(Sender: TObject);

begin

 _DllList:=TStringList.Create;

 LoadParams;

end;



procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);

begin

 SaveParams;

end;



procedure FormDestroy(Sender: TObject);

begin

 _DllList.Free;

end;



procedure TForm1.DllListAdd(FileName:String);

begin

 _DllList.Add(FileName);

 DllListBox.Items.Add(ExtractFileName(FileName));

end; 



procedure TForm1.DllListDelete(i:Integer);

begin

 if (i>=0) And (i<_DllList.Count) Then

  begin  

   _DllList.Delete(i);

   DllListBox.Items.Add(ExtractFileName(FileName));

  end; 

end; 



procedure TForm1.btnAddClick(Sender: TObject);

begin

  If DllOpenDialog.Execute Then DllListAdd(DllOpenDialog.Filename);  

end;



procedure TForm1.btnRemoveClick(Sender: TObject);

begin

 DllListDelete(DllListBox.ItemIndex);

end;



procedure TForm1.btnCloseClick(Sender: TObject);

begin

 Close;

end;



procedure TForm1.btnSaveClick(Sender: TObject);

begin

 SaveParams;

end;



procedure TForm1.LoadParams;

Var

 FileName:String;

 IniFile:TIniFile;

 i,N:integer;

begin

 FileName:=ChangeFileExt(Application.ExeName,'.INI'); 

 IniFile:=TIniFile.Create(FileName);

 N:=IniFile.ReadInteger('DLL_LIST','COUNT',0);

 ListBox1.Items.Clear;

 for i:=1 to N do 

  begin 

   FileName:=IniFile.ReadString ('DLL_LIST',IntToStr(i),'');

   if FileExists(FileName) Then DllListAdd(FileName);

  end;

 IniFile.Free;

end;



procedure TForm1.SaveParams;

Var

 FileName:String;

 IniFile:TIniFile;

 i,N:integer;

begin

 FileName:=ChangeFileExt(Application.ExeName,'.INI');

 IniFile:=TIniFile.Create(FileName);

 IniFile.WriteInteger('DLL_LIST','COUNT',DllListBox.Items.Count);

 for i:=0 to ListBox1.Items.Count-1 do 

  begin 

   IniFile.WriteString ('DLL_LIST',IntToStr(i+1),DllListBox.Items[i]);

  end;

 IniFile.Free;

end;



end.

Open in new window

0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:nkilla
ID: 33642980
Not you any malicious purposes just learning, I'm not wanting to trojans or the like lol.


Well I would list the processes in a Windows Form and icons appear in their processes and form by clicking on the selected process would be picked up by the Edit?

Thanks epasquier
0
 

Author Comment

by:nkilla
ID: 33643256
Build
  [Error] Unit1.pas(41): Undeclared identifier: '_DllList'
  [Error] Unit1.pas(42): Undeclared identifier: 'LoadParams'
  [Error] Unit1.pas(52): Undeclared identifier: '_DllList'
  [Error] Unit1.pas(55): Undeclared identifier: 'DllListAdd'
  [Error] Unit1.pas(57): Undeclared identifier: '_DllList'
  [Error] Unit1.pas(57): Undeclared identifier: 'FileName'
  [Error] Unit1.pas(58): Undeclared identifier: 'DllListBox'
  [Error] Unit1.pas(58): Missing operator or semicolon
  [Error] Unit1.pas(61): Undeclared identifier: 'DllListDelete'
  [Error] Unit1.pas(61): ';' expected but '(' found
  [Error] Unit1.pas(63): Undeclared identifier: 'i'
  [Error] Unit1.pas(63): Undeclared identifier: '_DllList'
  [Error] Unit1.pas(65): Missing operator or semicolon
  [Error] Unit1.pas(66): Undeclared identifier: 'DllListBox'
  [Error] Unit1.pas(66): Missing operator or semicolon
  [Error] Unit1.pas(66): Undeclared identifier: 'FileName'
  [Error] Unit1.pas(72): Undeclared identifier: 'DllListAdd'
  [Error] Unit1.pas(77): Undeclared identifier: 'DllListDelete'
  [Error] Unit1.pas(93): Undeclared identifier: 'TIniFile'
  [Error] Unit1.pas(97): Missing operator or semicolon
  [Error] Unit1.pas(97): Incompatible types: 'TComponent' and 'String'
  [Error] Unit1.pas(98): Missing operator or semicolon
  [Error] Unit1.pas(99): Undeclared identifier: 'ListBox1'
  [Error] Unit1.pas(99): Missing operator or semicolon
  [Error] Unit1.pas(102): Missing operator or semicolon
  [Error] Unit1.pas(103): Undeclared identifier: 'DllListAdd'
  [Error] Unit1.pas(105): Missing operator or semicolon
  [Error] Unit1.pas(111): Undeclared identifier: 'TIniFile'
  [Error] Unit1.pas(115): Missing operator or semicolon
  [Error] Unit1.pas(115): Incompatible types: 'TComponent' and 'String'
  [Error] Unit1.pas(116): Missing operator or semicolon
  [Error] Unit1.pas(117): Undeclared identifier: 'ListBox1'
  [Error] Unit1.pas(119): Missing operator or semicolon
  [Error] Unit1.pas(121): Missing operator or semicolon
  [Error] Unit1.pas(21): Unsatisfied forward or external declaration: 'TForm1.FormCreate'
  [Error] Unit1.pas(23): Unsatisfied forward or external declaration: 'TForm1.FormDestroy'
  [Hint] Unit1.pas(26): Private symbol '_DllList' declared but never used
  [Hint] Unit1.pas(28): Private symbol 'LoadParams' declared but never used
  [Fatal Error] Project1.dpr(5): Could not compile used unit 'Unit1.pas'



Errors
0
 
LVL 25

Expert Comment

by:epasquier
ID: 33644129
I forgot the "TForm1." in the implementation of FormCreate & FormDestroy

and for Load/Save I have remnants of older code. I was tired when I posted that :o)
procedure TForm1.FormCreate(Sender: TObject);

begin

 _DllList:=TStringList.Create;

 LoadParams;

end;



procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);

begin

 SaveParams;

end;



procedure TForm1.FormDestroy(Sender: TObject);

begin

 _DllList.Free;

end;



procedure TForm1.LoadParams;

Var

 FileName:String;

 IniFile:TIniFile;

 i,N:integer;

begin

 FileName:=ChangeFileExt(Application.ExeName,'.INI'); 

 IniFile:=TIniFile.Create(FileName);

 N:=IniFile.ReadInteger('DLL_LIST','COUNT',0);

 DllListBox.Items.Clear;

 _DllList.Clear;

 for i:=1 to N do 

  begin 

   FileName:=IniFile.ReadString ('DLL_LIST',IntToStr(i),'');

   if FileExists(FileName) Then DllListAdd(FileName);

  end;

 IniFile.Free;

end;



procedure TForm1.SaveParams;

Var

 FileName:String;

 IniFile:TIniFile;

 i,N:integer;

begin

 FileName:=ChangeFileExt(Application.ExeName,'.INI');

 IniFile:=TIniFile.Create(FileName);

 IniFile.WriteInteger('DLL_LIST','COUNT',DllListBox.Items.Count);

 for i:=0 to DllListBox.Items.Count-1 do 

  begin 

   IniFile.WriteString ('DLL_LIST',IntToStr(i+1),DllListBox.Items[i]);

  end;

 IniFile.Free;

end;

Open in new window

0
 

Author Comment

by:nkilla
ID: 33651680
Build
  [Error] Unit1.pas(44): Undeclared identifier: 'LoadParams'
  [Error] Unit1.pas(57): Undeclared identifier: 'DllListAdd'
  [Error] Unit1.pas(59): Undeclared identifier: 'FileName'
  [Error] Unit1.pas(60): Undeclared identifier: 'DllListBox'
  [Error] Unit1.pas(60): Missing operator or semicolon
  [Error] Unit1.pas(63): Undeclared identifier: 'DllListDelete'
  [Error] Unit1.pas(63): ';' expected but '(' found
  [Error] Unit1.pas(65): Undeclared identifier: 'i'
  [Error] Unit1.pas(68): Undeclared identifier: 'DllListBox'
  [Error] Unit1.pas(68): Missing operator or semicolon
  [Error] Unit1.pas(68): Undeclared identifier: 'FileName'
  [Error] Unit1.pas(74): Undeclared identifier: 'DllListAdd'
  [Error] Unit1.pas(79): Undeclared identifier: 'DllListDelete'
  [Error] Unit1.pas(95): Undeclared identifier: 'TIniFile'
  [Error] Unit1.pas(99): Missing operator or semicolon
  [Error] Unit1.pas(99): Incompatible types: 'TComponent' and 'String'
  [Error] Unit1.pas(100): Missing operator or semicolon
  [Error] Unit1.pas(105): Missing operator or semicolon
  [Error] Unit1.pas(106): Undeclared identifier: 'DllListAdd'
  [Error] Unit1.pas(108): Missing operator or semicolon
  [Error] Unit1.pas(114): Undeclared identifier: 'TIniFile'
  [Error] Unit1.pas(118): Missing operator or semicolon
  [Error] Unit1.pas(118): Incompatible types: 'TComponent' and 'String'
  [Error] Unit1.pas(119): Missing operator or semicolon
  [Error] Unit1.pas(122): Missing operator or semicolon
  [Error] Unit1.pas(124): Missing operator or semicolon
  [Error] Unit1.pas(21): Unsatisfied forward or external declaration: 'TForm1.FormCreate'
  [Error] Unit1.pas(23): Unsatisfied forward or external declaration: 'TForm1.FormDestroy'
  [Fatal Error] Project1.dpr(5): Could not compile used unit 'Unit1.pas'



Can u send me a source compiled ?
This code have a many errors
0
 

Author Comment

by:nkilla
ID: 33658996
Can you correct you code for me please ?

Thanks for Helping
0
 
LVL 7

Expert Comment

by:samenglish
ID: 33659098
What Operating System are you using? Which version of Delphi are you using? How many years programming experience do you have?
0
 
LVL 25

Accepted Solution

by:
epasquier earned 500 total points
ID: 33660150
Here you go, complete and tested
unit Unit1;



interface



uses

  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,

  StdCtrls;



type

  TForm1 = class(TForm)

    DllListBox: TListBox;

    btnAdd: TButton;

    btnRemove: TButton;

    OpenDialog: TOpenDialog;

    procedure btnAddClick(Sender: TObject);

    procedure btnRemoveClick(Sender: TObject);

    procedure FormCreate(Sender: TObject);

    procedure FormClose(Sender: TObject; var Action: TCloseAction);

    procedure FormDestroy(Sender: TObject);

  private

    _DllList:TStringList;



    procedure DllListAdd(FileName:String);

    procedure DllListDelete(i:Integer);



    procedure LoadParams;

    procedure SaveParams;

  public

  end;



var

  Form1: TForm1;



implementation



Uses IniFiles;



{$R *.dfm}



procedure TForm1.FormCreate(Sender: TObject);

begin

 _DllList:=TStringList.Create;

 LoadParams;

end;



procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);

begin

 SaveParams;

end;



procedure TForm1.FormDestroy(Sender: TObject);

begin

 _DllList.Free;

end;



procedure TForm1.DllListAdd(FileName:String);

begin

 _DllList.Add(FileName);

 DllListBox.Items.Add(ExtractFileName(FileName));

end;



procedure TForm1.DllListDelete(i:Integer);

begin

 if (i>=0) And (i<_DllList.Count) Then

  begin  

   _DllList.Delete(i);

   DllListBox.Items.Delete(i);

  end;

end; 



procedure TForm1.btnAddClick(Sender: TObject);

begin

  If OpenDialog.Execute Then DllListAdd(OpenDialog.Filename);

end;



procedure TForm1.btnRemoveClick(Sender: TObject);

begin

 DllListDelete(DllListBox.ItemIndex);

end;



procedure TForm1.LoadParams;

Var

 FileName:String;

 IniFile:TIniFile;

 i,N:integer;

begin

 FileName:=ChangeFileExt(Application.ExeName,'.INI'); 

 IniFile:=TIniFile.Create(FileName);

 N:=IniFile.ReadInteger('DLL_LIST','COUNT',0);

 DllListBox.Items.Clear;

 for i:=1 to N do 

  begin 

   FileName:=IniFile.ReadString ('DLL_LIST',IntToStr(i),'');

   if FileExists(FileName) Then DllListAdd(FileName);

  end;

 IniFile.Free;

end;



procedure TForm1.SaveParams;

Var

 FileName:String;

 IniFile:TIniFile;

 i,N:integer;

begin

 FileName:=ChangeFileExt(Application.ExeName,'.INI');

 IniFile:=TIniFile.Create(FileName);

 IniFile.WriteInteger('DLL_LIST','COUNT',DllListBox.Items.Count);

 for i:=0 to _DllList.Count-1 do

  begin

   IniFile.WriteString ('DLL_LIST',IntToStr(i+1),_DllList[i]);

  end;

 IniFile.Free;

end;



end.



//=== DFM FILE



object Form1: TForm1

  Left = 412

  Top = 122

  Width = 1305

  Height = 675

  Caption = 'Form1'

  Color = clBtnFace

  Font.Charset = DEFAULT_CHARSET

  Font.Color = clWindowText

  Font.Height = -11

  Font.Name = 'MS Sans Serif'

  Font.Style = []

  OldCreateOrder = False

  OnClose = FormClose

  OnCreate = FormCreate

  OnDestroy = FormDestroy

  PixelsPerInch = 96

  TextHeight = 13

  object DllListBox: TListBox

    Left = 24

    Top = 56

    Width = 257

    Height = 361

    ItemHeight = 13

    TabOrder = 0

  end

  object btnAdd: TButton

    Left = 312

    Top = 56

    Width = 75

    Height = 25

    Caption = 'Add'

    TabOrder = 1

    OnClick = btnAddClick

  end

  object btnRemove: TButton

    Left = 312

    Top = 96

    Width = 75

    Height = 25

    Caption = 'Remove'

    TabOrder = 2

    OnClick = btnRemoveClick

  end

  object OpenDialog: TOpenDialog

    Left = 184

    Top = 32

  end

end

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

708 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

15 Experts available now in Live!

Get 1:1 Help Now