[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

ListBox problem

Posted on 1999-01-03
4
Medium Priority
?
1,576 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 200 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

825 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