Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

working with 2 listboxes

Posted on 2003-12-01
9
333 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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
 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
select query - oracle 16 100
Convert a string into a TDateTime 5 64
CheckListBox usage 3 71
How to add columns to a RAD Studio FMX grid at design time 1 4
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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…

837 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