2 List Boxes - Select item in one to then appear in listbox2

I am trying to write the code to be able to select an item in one
listbox and then have that item appear in a second list box. I do
not want more than 20 items to be selected from first listbox.
James_NapierAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
shaneholmesConnect With a Mentor Commented:
procedure TForm1.Button1Click(Sender: TObject);
var
 I: Integer;
begin
 for I:= 0 to ListBox1.Count - 1 do
  if ListBox1.Selected[I] then
    ListBox2.Items.Add(listBox1.Items[I]);
end;

procedure TForm1.ListBox1Click(Sender: TObject);
var
 I: Integer;
begin
 I:= listbox1.ItemIndex;
 if listbox1.SelCount > 20 then
  ListBox1.Selected[I]:= False;
end;
0
 
mokuleCommented:
Hi
1. Do You want to select max 20 items at once or one at a time?
2. When do You want the selected item appear in the second listbox?
0
 
shaneholmesCommented:
for I:= 0 to ListBox1.Count - 1 do
  if ListBox1.Selected[I] then
    ListBox2.Items.Add(listBox1.Items[I]);


Shane
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 
James_NapierAuthor Commented:
Many thanks - would it make any difference if I
am using a DBListbox as listbox1?
0
 
shaneholmesCommented:
Nope, I dont believe so, it has the same properties..... Although, i didn't test it... im sure it wont matter

Shane
0
 
mokuleCommented:
With DBListbox it absolutly don't work.
0
 
James_NapierAuthor Commented:
Agree - it doesn't seem to work. What would work in this case? I am transferring data
form a dbcontrolgrid into dblistbox1 and now need to allow users to select items in
dblistbox1(maximum total selection not greater than 20) and have then transfered to
a second listbox.
0
 
shaneholmesCommented:
Sorry, hes right, there is no Multiselect ---- <SMILE>

Shane
0
 
mokuleCommented:
Not quite.
Multiselect can be set at runtime.
What I mean was the trouble with unwanted change of data in database.
0
 
shaneholmesCommented:
Right,  multiselect can be used at runtime, if you use the Selection array
however, you can't physically select from the OnClick event....and show the highlighted selections

designed for one selection .....


Shane
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.