We help IT Professionals succeed at work.

Listbox Manipulating

nkilla
nkilla asked
on
1,055 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
Comment
Watch Question

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

Oh... set Multiselect = True on the ListBox and you can select multiple items with your mouse while holding down the Ctrl key.
Emmanuel PASQUIERFreelance Project Manager
CERTIFIED EXPERT
Top Expert 2010

Commented:
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

Author

Commented:
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
Emmanuel PASQUIERFreelance Project Manager
CERTIFIED EXPERT
Top Expert 2010

Commented:
> 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
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. :-)
Emmanuel PASQUIERFreelance Project Manager
CERTIFIED EXPERT
Top Expert 2010

Commented:
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

Author

Commented:
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

Author

Commented:
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
Emmanuel PASQUIERFreelance Project Manager
CERTIFIED EXPERT
Top Expert 2010

Commented:
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

Author

Commented:
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

Author

Commented:
Can you correct you code for me please ?

Thanks for Helping
What Operating System are you using? Which version of Delphi are you using? How many years programming experience do you have?
Freelance Project Manager
CERTIFIED EXPERT
Top Expert 2010
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.