Solved

ListBox problem

Posted on 1999-01-03
4
1,565 Views
Last Modified: 2012-06-27
Hi...  
  I'm using Delphi 3 and I have an app that uses a TListBox with the sorted property set to true. I have it coded so that I can double-click on any item in the ListBox and I can edit it using a simple inputbox. That works ok, problem is, after I edit an item and change it to something else, the list does not automatically re-sort itself. The new edited item stays just where it was before even though it is now out of alphabetical order! Can you help?
  In a related question concerning TListBoxes...can they be set so that no duplicate items can be entered? Or can this only be done with a TStringList?
 
Thanks
  Shawn Halfpenny
  drumme59@sprint.ca
0
Comment
Question by:aztec
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
slautin earned 50 total points
ID: 1354333
for resorted use:

var i : integer;
    s : string;
begin
    s := ListBox1.Items[ListBox1.ItemIndex];
    ListBox1.Sorted:=False;
    ListBox1.Sorted:=True;
    for i := 1 to ListBox1.ItemIndex do
       if ListBox1.Items[i] := s then
           break;
    ListBox1.ItemIndex := i;

For no duplicate:

var i : integer;
    can_add : boolean;
begin
    can_add := true;
    for i := 1 to ListBox1.ItemIndex do
       begin
       if ListBox1.Items[i] := newval then
           begin
           can_add := false;
           break;
           end;
       end;
    if can_add then
       ListBox1.Items.Add[newval];

......

0
 
LVL 10

Expert Comment

by:viktornet
ID: 1354334
Here is the simplest way,,,

ListBox.Items[ListBox.ItemIndex] := 'Whatever';
ListBox.Sorted := False;
ListBox.Sorted := True;

Now everything would be sorted....

If oyu dont want to have duplicates in your listbox as slautin prposed, you can do one of two ways....

the first is this,,,,

if ListBox.Items.IndexOf('Whatever') < 0 then begin
  ListBox.Items[ListBox.ItemIndex] := 'Whatever';
  ListBox.Sorted := False;
  ListBox.Sorted := True;
end;

This was the first way w/o having duaplicates.... The second way is to do this...

var
  List : TStringList;
begin
  List := TStringList.Create;
  List.Assign(ListBox.Items);
  List.Duplicates := dupIgnore;
  List.Sort := True;
  ListBox.Items.Assign(List);
  List.Free;
end;

This would remove the duplicates and will sort the items and put them back in the listbox...

Hope this helps...

-Viktor
--Ivanov
0
 

Author Comment

by:aztec
ID: 1354335
Great Thanks! And thanks to Viktor for his comments.

Shawn
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1354336
No problem... If you are still having problems let me know...
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

861 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