Solved

working with 2 listboxes

Posted on 2003-12-01
9
328 Views
Last Modified: 2010-04-05
Dear friends,

1. I create two string list editor. Ok?

However in my form would appear an option for which list must be appear (in a combobox) for example.

When the user choice one or another, that list will appear in a combobox. Now the user click one item and appear in a memo the
item of another list in the same line.

In resume: I am wroking with two lists, but only only the user see.
When the user click one item, at side appear a memo with the item (same line) of another list.

Best regards.

janlee
0
Comment
Question by:janlee
  • 4
  • 4
9 Comments
 
LVL 9

Expert Comment

by:mocarts
ID: 9850090
hi, janlee :)

comboBox example (written from head - so there can be compile time or runtime errors, but you can get main idea from this code):

var
  s1, s2: TStringList;

procedure TForm1.FormCreate(Sender: TObject);
begin
  // load comboboxes from string lists
  ComboBox1.Items.Assign(s1);
  ComboBox2.Items.Assign(s2);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  // save items
  s1.Assign(ComboBox1.Items);
  s2.Assign(ComboBox2.Items);
end;

// OnChange for combobox1 (same must be implemented for combobox2)
procedure TForm1.ComboBox1Change(Sender: TObject);
begin
  if (ComboBox1.ItemIndex > -1) and   (ComboBox1.ItemIndex < ComboBox2.Items.Count) then
    Edit1.Lines.Text := ComboBox2.Items[ComboBox1.ItemIndex]
  else
    Edit1.Lines.Text := '< unassigned >';
  Edit1.Tag := integer(ComboBox2); // current edited combobox items
end;

procedure TForm1.Edit1Exit(Sender: TObject);
begin
  // save current item changes, if any
  if Edit1.Modified and (Edit1.Tag <> 0) and (TComboBox(Edit1.Tag).ItemIndex > -1) then
  begin
    TComboBox(Edit1.Tag).Items[TComboBox(Edit1.Tag).ItemIndex] := Edit1.Text;
    Edit1.Modified := False;
  end;
end;

wbr, mo.
0
 
LVL 1

Expert Comment

by:roknjohn
ID: 9851265
1) are you using two TListBox controls and toggling their visiblity depending on the user's selection in the TComboBox, or are you using just one TListBox and loading its contents based on the TComboBox selection?

2) Are you saying that you DO or DO NOT want the string from the other list to display in the memo?

If you post your code, we can help you much faster.
0
 

Author Comment

by:janlee
ID: 9852035
Please, there are misspellings in your script such as

Edit1.Lines.Text
2 formcreate

jl
0
 
LVL 1

Accepted Solution

by:
roknjohn earned 150 total points
ID: 9852198
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    ComboBox1: TComboBox;
    ListBox1: TListBox;
    Edit1: TEdit;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);
    procedure ListBox1Click(Sender: TObject);
    procedure Edit1Change(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    List1, List2 : TStringList;  //these are your lists to edit
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  List1 := TStringList.Create;
  List2 := TStringList.Create;

  //Populate the lists from somewhere, like:
  List1.LoadFromFile('C:\List1.txt');
  List2.LoadFromFile('C:\List2.txt');

  //Populate the combobox, is not done at design time
  ComboBox1.Items.Add('List 1');
  ComboBox1.Items.Add('List 2');
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  List2.Free;   //You'll probably want to save these lists before freeing them.  
  List1.Free;  //Do that in a button OnClick or the forms OnClose event
end;

procedure TForm1.ComboBox1Change(Sender: TObject);
begin
  case ComboBox1.ItemIndex of                  //show the selected list
    0 : ListBox1.Items.Assign(List1);
    1 : ListBox1.Items.Assign(List2);
  end;
end;

procedure TForm1.ListBox1Click(Sender: TObject);
begin
  case ComboBox1.ItemIndex of                     //show the correlated item of the selected item in the edit box
    0 : Edit1.Text := List2[ListBox1.ItemIndex];
    1 : Edit1.Text := List1[ListBox1.ItemIndex];
  end;
end;

procedure TForm1.Edit1Change(Sender: TObject);
begin
  case ComboBox1.ItemIndex of                          //update the list of any user changes to the item
    0 : List2[ListBox1.ItemIndex] := Edit1.Text;
    1 : List1[ListBox1.ItemIndex] := Edit1.Text;
  end;
end;

end.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:janlee
ID: 9853903
Dear RockHohn,

Congratulations. Your script works very, very fine.

However my intention is hidden the source of data, in this way I was thought in use the ´string list editor´ . Would be possible using
your unique combobox create a stringlist in this way:

ONE,UNO
TWO,DEUX
THREE,TROIS
FOUR,CATRE
FIVE,CINQUE

Where the words before the comma would be ´list1´ and words after comma would be ´list2´.

I will put more points.

Many thanks.

janlee


You win
0
 

Author Comment

by:janlee
ID: 9857309
Dear RockJohn,

I forgot to mention there is a problem with your script. A little problem.
The lists are not indexed. It is possible? I  know it is possible sort the items of a listbox, however and in another list?

regards,

jl
0
 
LVL 1

Expert Comment

by:roknjohn
ID: 9858492
janlee,

It is fairly easy to split an input file of:

ONE,UNO
TWO,DEUX
THREE,TROIS
FOUR,CATRE
FIVE,CINQUE

into 2 stringlists of ONE, TWO..FIVE and UNO, DEUX, CINQUE.

procedure LoadLists(InputFile : string; List1, List2 : TStringList);
var
  i, p : integer;
  s : string;
begin
  List2.LoadFromFile(InputFile);
  List1.Clear;
  for i := 0 to List2.Count-1 do begin
    s := List2[i];
    p := Pos(',',s);
    List1.Add(Copy(List2[i],1,p-1));
    Delete(s,1,p);
    List2[i] := s;
  end;
end;
0
 
LVL 1

Expert Comment

by:roknjohn
ID: 9858664
janlee,

About indexing.  If you need to sort one or both of the lists while preserving the relationship between the items, you can use the Objects property, and store the original index value, typecast as a pointer.

After the lists are loaded, do something like:

  for i := 0 to List1.Count-1 do begin
    List1.Objects[i] := Pointer(i);
    List2.Objects[i] := Pointer(i);
  end;

Then, change the following event handlers to look like:

procedure TForm1.ListBox1Click(Sender: TObject);
var
  ix : Pointer;
begin
  ix := ListBox1.Items.Objects[ListBox1.ItemIndex];
  case ComboBox1.ItemIndex of
    0 : Edit1.Text := List2[List2.IndexOfObject(ix)];
    1 : Edit1.Text := List1[List1.IndexOfObject(ix)];
  end;
end;

procedure TForm1.Edit1Change(Sender: TObject);
var
  ix : Pointer;
begin
  ix := ListBox1.Items.Objects[ListBox1.ItemIndex];
  case ComboBox1.ItemIndex of
    0 : List2[List2.IndexOfObject(ix)] := Edit1.Text;
    1 : List1[List1.IndexOfObject(ix)] := Edit1.Text;
  end;
end;

Now you can sort either list.  Hope this helps.
0
 

Author Comment

by:janlee
ID: 9859187
Ok. Now it is working very, very fine.

Thank you very much.

Best Regards.

janlee
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 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

19 Experts available now in Live!

Get 1:1 Help Now