Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Moving Multiple Items ListBox To ListBox

Status
Not open for further replies.

PaidtheUmpire

Programmer
Jan 4, 2004
105
AU
I'm trying to do the following, if you have any exmaples of code OR any help at all... my thanks goes out to you.

LISTBOX 1
ItemA
ItemC

ListBox2
ItemB
ItemD
ItemE
ItemF

What i want tobe able to do is move items from one listbox to the other, and then sorting the updated listboxs alphabetically. In the above example, i want to move Items B, D & F from ListBox2 to ListBox1. So I'd select the multiple rows and then click the move button... the items would move across and the two listbox lists would sort alphabtically. What I'm having trouble doing is the whole code to a) move multiple listbox items across, b) sort the listbox items after woulds.

Thanks for your help.

Delphi, Delphi, Delphi. Oi! Oi! Oi!
 
Code:
for i := 0 to Listbox2.Items.Count-1 do                              
  if Listbox2.Selected[i] then
    Listbox1.Items.Add(Items.Strings[i]);

Listbox2.items.clear;
Listbox1.sorted:=true;

- Ryan
 
Ryan,

Your example deletes all the items in ListBox2. The original request was, I think, to delete only the items which were moved to ListBox1.

Somthing like the following should work:
Code:
for i := ListBox2.Items.Count - 1 downto 0 do
  if ListBox2.Selected[i] then begin
    ListBox1.Items.Add(ListBox2.Items[i]);
    ListBox2.Items.Delete(i);
  end;
ListBox1.Sorted := true;
ListBox2.Sorted := true;
I've not tested the above so there may be some typos.

Andrew
Hampshire, UK
 
PaidtheUmpire said:
What I'm having trouble doing is the whole code to a) move multiple listbox items across, b) sort the listbox items after woulds.

PaidtheUmpire,
Did you actually look in the Delphi help files before posting your question?

Regarding (b), I opened the help file and in the Index typed "Sorted", a short list appeared which showed TCustomListBox, I clicked this entry and the Sorted property was explained and a link to an example was provided! I also typed "TListBox" in the Index, one entry matched, I clicked it, a page appeared explaining TListBox, I clicked Properties and Sorted was clearly visible in the list!

I am not trying to cause offence, but the best way to learn Delphi is to do some digging yourself, rather than posting in here straight away. Always go to the Delphi help file first - it is generally pretty good and it's pretty easy to find what you're looking for.

The second step is to try google if you have no luck in the Delphi help file. I always search with a "delphi" prefix. So for (b) I would type something like "delphi tlistbox sort" and variations of that.

With this particular example, I would have looked up TListBox and looked for methods or properties that add and remove items to/from listboxes to answer (a). And I would have looked for some kind of Sort method or property to answer (b).

Clive [infinity]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer."
Paul Ehrlich
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top